极简三位数加减出题软件
暑假帮妹妹在预习三年级的数学,我觉得每天要给她出算术题批改太麻烦了= =
所以今天花了半天给妹妹做了个安卓端的简单出题软件让她装她手机上。
思路就是:妹妹打开软件,点击按钮生成题目,她把题目抄在本子上列竖式计算,最后再让她点击答案自己批改。
科技改变生活啊!算术书都不用买了2333333
界面什么的都很简单:
代码也非常简单:
MainActivity.java
1 package com.example.studentc; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.TextView; 11 12 13 public class MainActivity extends Activity { 14 private TextView[] a; //使用数组批量使用id 15 private TextView textViewKey; 16 private Button button1; 17 private Button button2; 18 int[] key = new int[20]; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 a = new TextView[20]; 25 int[] textViewID = new int[] { R.id.textView1, R.id.textView2, 26 R.id.textView3, R.id.textView4, R.id.textView5, R.id.textView6, 27 R.id.textView7, R.id.textView8, R.id.textView9, 28 R.id.textView10, R.id.textView11, R.id.textView12, 29 R.id.textView13, R.id.textView14, R.id.textView15, 30 R.id.textView16, R.id.textView17, R.id.textView18, 31 R.id.textView19, R.id.textView20 }; 32 for (int i = 0; i < 20; i++) { 33 a[i] = (TextView) findViewById(textViewID[i]); 34 } 35 textViewKey = (TextView)findViewById(R.id.textView21); 36 button1 = (Button)findViewById(R.id.button1); 37 button2 = (Button)findViewById(R.id.button2); 38 39 OnClickButtonListener listener = new OnClickButtonListener(); 40 OnClickKeyButtonListener listener2 = new OnClickKeyButtonListener(); 41 button1.setOnClickListener(listener); 42 button2.setOnClickListener(listener2); 43 } 44 45 class OnClickButtonListener implements OnClickListener{ 46 @Override 47 public void onClick(View v) { 48 int first; 49 int second; 50 51 textViewKey.setText("KEY: "); 52 53 for (int i = 0; i < 10; i++) { 54 first = (int) (Math.random() * 1000); 55 second = (int) (Math.random() * 1000); 56 Formula formula = new Formula(first, "+", second); 57 a[i].setText("(" + (i + 1) + ")" + formula.getFirst() 58 + formula.getSign() + formula.getSecond() + "="); 59 key[i] = formula.getFirst() + formula.getSecond(); 60 } 61 for (int i = 0; i < 10; i++) { 62 first = (int) (Math.random() * 1000); 63 second = (int) (Math.random() * 1000); 64 Formula formula = new Formula(first, "-", second); 65 if (first < second) { 66 int t = formula.getFirst(); 67 formula.setFirst(formula.getSecond()); 68 formula.setSecond(t); 69 } 70 a[10 + i].setText("(" + (i + 11) + ")" + formula.getFirst() 71 + formula.getSign() + formula.getSecond() + "="); 72 key[10 + i] = formula.getFirst() - formula.getSecond(); 73 } 74 button2.setEnabled(true); 75 } 76 } 77 78 class OnClickKeyButtonListener implements OnClickListener{ 79 @Override 80 public void onClick(View v) { 81 int count=0; 82 for(int i=0;i<20;i++){ 83 count++; 84 if(count%3==0){ 85 textViewKey.append("\n"); 86 } 87 textViewKey.append("("+(i+1)+")"+key[i]+" "); 88 } 89 button2.setEnabled(false); 90 } 91 } 92 93 @Override 94 public boolean onCreateOptionsMenu(Menu menu) { 95 getMenuInflater().inflate(R.menu.main, menu); 96 return true; 97 } 98 99 @Override 100 public boolean onOptionsItemSelected(MenuItem item) { 101 int id = item.getItemId(); 102 if (id == R.id.action_settings) { 103 return true; 104 } 105 return super.onOptionsItemSelected(item); 106 } 107 }
*写代码的过程里,学习到了使用数组批量引用ID
activity_main.xml:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.studentc.MainActivity" > 10 11 <LinearLayout 12 android:id="@+id/linearLayout1" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:orientation="horizontal" > 16 17 <LinearLayout 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_weight="1" 21 android:orientation="vertical" > 22 23 <TextView 24 android:id="@+id/textView1" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:layout_alignParentLeft="true" 28 android:layout_alignParentTop="true" 29 android:layout_marginBottom="3dp" 30 android:text=" " 31 android:textSize="16sp" /> 32 33 <TextView 34 android:id="@+id/textView2" 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" 37 android:layout_alignRight="@+id/textView1" 38 android:layout_below="@+id/textView1" 39 android:layout_marginBottom="3dp" 40 android:text=" " 41 android:textSize="16sp" /> 42 43 <TextView 44 android:id="@+id/textView3" 45 android:layout_width="match_parent" 46 android:layout_height="wrap_content" 47 android:layout_alignLeft="@+id/textView2" 48 android:layout_below="@+id/textView2" 49 android:layout_marginBottom="3dp" 50 android:text=" " 51 android:textSize="16sp" /> 52 53 <TextView 54 android:id="@+id/textView4" 55 android:layout_width="match_parent" 56 android:layout_height="wrap_content" 57 android:layout_alignRight="@+id/textView3" 58 android:layout_below="@+id/textView3" 59 android:layout_marginBottom="3dp" 60 android:text=" " 61 android:textSize="16sp" /> 62 63 <TextView 64 android:id="@+id/textView5" 65 android:layout_width="match_parent" 66 android:layout_height="wrap_content" 67 android:layout_alignRight="@+id/textView4" 68 android:layout_below="@+id/textView4" 69 android:layout_marginBottom="3dp" 70 android:text=" " 71 android:textSize="16sp" /> 72 73 <TextView 74 android:id="@+id/textView6" 75 android:layout_width="match_parent" 76 android:layout_height="wrap_content" 77 android:layout_alignRight="@+id/textView5" 78 android:layout_below="@+id/textView5" 79 android:layout_marginBottom="3dp" 80 android:text=" " 81 android:textSize="16sp" /> 82 83 <TextView 84 android:id="@+id/textView7" 85 android:layout_width="match_parent" 86 android:layout_height="wrap_content" 87 android:layout_alignLeft="@+id/textView6" 88 android:layout_below="@+id/textView6" 89 android:layout_marginBottom="3dp" 90 android:text=" " 91 android:textSize="16sp" /> 92 93 <TextView 94 android:id="@+id/textView8" 95 android:layout_width="match_parent" 96 android:layout_height="wrap_content" 97 android:layout_alignRight="@+id/textView7" 98 android:layout_below="@+id/textView7" 99 android:layout_marginBottom="3dp" 100 android:text=" " 101 android:textSize="16sp" /> 102 103 <TextView 104 android:id="@+id/textView9" 105 android:layout_width="match_parent" 106 android:layout_height="wrap_content" 107 android:layout_alignLeft="@+id/textView8" 108 android:layout_below="@+id/textView8" 109 android:layout_marginBottom="3dp" 110 android:text=" " 111 android:textSize="16sp" /> 112 113 <TextView 114 android:id="@+id/textView10" 115 android:layout_width="match_parent" 116 android:layout_height="wrap_content" 117 android:layout_alignLeft="@+id/textView9" 118 android:layout_below="@+id/textView9" 119 android:layout_marginBottom="3dp" 120 android:text=" " 121 android:textSize="16sp" /> 122 </LinearLayout> 123 124 <LinearLayout 125 android:layout_width="wrap_content" 126 android:layout_height="wrap_content" 127 android:layout_weight="1" 128 android:orientation="vertical" > 129 130 <TextView 131 android:id="@+id/textView11" 132 android:layout_width="match_parent" 133 android:layout_height="wrap_content" 134 android:layout_alignParentLeft="true" 135 android:layout_alignParentTop="true" 136 android:layout_marginBottom="3dp" 137 android:text=" " 138 android:textSize="16sp" /> 139 140 <TextView 141 android:id="@+id/textView12" 142 android:layout_width="match_parent" 143 android:layout_height="wrap_content" 144 android:layout_alignRight="@+id/textView1" 145 android:layout_below="@+id/textView1" 146 android:layout_marginBottom="3dp" 147 android:text=" " 148 android:textSize="16sp" /> 149 150 <TextView 151 android:id="@+id/textView13" 152 android:layout_width="match_parent" 153 android:layout_height="wrap_content" 154 android:layout_alignLeft="@+id/textView2" 155 android:layout_below="@+id/textView2" 156 android:layout_marginBottom="3dp" 157 android:text=" " 158 android:textSize="16sp" /> 159 160 <TextView 161 android:id="@+id/textView14" 162 android:layout_width="match_parent" 163 android:layout_height="wrap_content" 164 android:layout_alignRight="@+id/textView3" 165 android:layout_below="@+id/textView3" 166 android:layout_marginBottom="3dp" 167 android:text=" " 168 android:textSize="16sp" /> 169 170 <TextView 171 android:id="@+id/textView15" 172 android:layout_width="match_parent" 173 android:layout_height="wrap_content" 174 android:layout_alignRight="@+id/textView4" 175 android:layout_below="@+id/textView4" 176 android:layout_marginBottom="3dp" 177 android:text=" " 178 android:textSize="16sp" /> 179 180 <TextView 181 android:id="@+id/textView16" 182 android:layout_width="match_parent" 183 android:layout_height="wrap_content" 184 android:layout_alignRight="@+id/textView5" 185 android:layout_below="@+id/textView5" 186 android:layout_marginBottom="3dp" 187 android:text=" " 188 android:textSize="16sp" /> 189 190 <TextView 191 android:id="@+id/textView17" 192 android:layout_width="match_parent" 193 android:layout_height="wrap_content" 194 android:layout_alignLeft="@+id/textView6" 195 android:layout_below="@+id/textView6" 196 android:layout_marginBottom="3dp" 197 android:text=" " 198 android:textSize="16sp" /> 199 200 <TextView 201 android:id="@+id/textView18" 202 android:layout_width="match_parent" 203 android:layout_height="wrap_content" 204 android:layout_alignRight="@+id/textView7" 205 android:layout_below="@+id/textView7" 206 android:layout_marginBottom="3dp" 207 android:text=" " 208 android:textSize="16sp" /> 209 210 <TextView 211 android:id="@+id/textView19" 212 android:layout_width="match_parent" 213 android:layout_height="wrap_content" 214 android:layout_alignLeft="@+id/textView8" 215 android:layout_below="@+id/textView8" 216 android:layout_marginBottom="3dp" 217 android:text=" " 218 android:textSize="16sp" /> 219 220 <TextView 221 android:id="@+id/textView20" 222 android:layout_width="match_parent" 223 android:layout_height="wrap_content" 224 android:layout_alignLeft="@+id/textView9" 225 android:layout_below="@+id/textView9" 226 android:layout_marginBottom="3dp" 227 android:text=" " 228 android:textSize="16sp" /> 229 </LinearLayout> 230 </LinearLayout> 231 232 <TextView 233 android:id="@+id/textView21" 234 android:layout_width="match_parent" 235 android:layout_height="wrap_content" 236 android:layout_alignLeft="@+id/linearLayout1" 237 android:layout_below="@+id/linearLayout1" 238 android:layout_margin="10dp" 239 android:text=" 欢迎许佳懿小朋友来做算术!!!^0^" 240 android:textSize="16sp"/> 241 242 <RelativeLayout 243 android:layout_width="wrap_content" 244 android:layout_height="wrap_content" 245 android:layout_alignParentBottom="true" 246 android:layout_centerHorizontal="true" > 247 248 <Button 249 android:id="@+id/button1" 250 android:layout_width="wrap_content" 251 android:layout_height="wrap_content" 252 android:text="生成" /> 253 254 <Button 255 android:id="@+id/button2" 256 android:layout_width="wrap_content" 257 android:layout_height="wrap_content" 258 android:layout_below="@id/button1" 259 android:text="答案" /> 260 </RelativeLayout> 261 262 </RelativeLayout>
Formula类:
1 package com.example.studentc; 2 3 public class Formula { 4 private int first; 5 private String sign; 6 private int second; 7 public Formula(){} 8 public Formula(int first, String sign, int second){ 9 this.first = first; 10 this.sign = sign; 11 this.second = second; 12 } 13 public int getFirst() { 14 return first; 15 } 16 public void setFirst(int first) { 17 this.first = first; 18 } 19 public String getSign() { 20 return sign; 21 } 22 public void setSign(String sign) { 23 this.sign = sign; 24 } 25 public int getSecond() { 26 return second; 27 } 28 29 public void setSecond(int second) { 30 this.second = second; 31 } 32 }
*在老爸的手机上试了下,发现内容装不下,差了些资料关于android实现上下滑动:
在布局最外包一层滚动条
<ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </ScrollView>