具体见小伙伴博客
version 1.0
http://www.cnblogs.com/ly199553/p/5339231.html
version 2.0
http://www.cnblogs.com/ly199553/p/5351593.html
version 3.0
首先贴出界面UI
源代码:
主程序MainActivity
1 package com.team.randomcal; 2 3 4 5 import java.util.Timer; 6 import java.util.TimerTask; 7 8 import android.os.Bundle; 9 10 import android.app.Activity; 11 import android.content.Intent; 12 import android.view.Menu; 13 import android.view.Window; 14 import android.view.WindowManager; 15 import android.webkit.WebView; 16 17 public class MainActivity extends Activity { 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 //去掉标题栏 23 requestWindowFeature(Window.FEATURE_NO_TITLE); 24 //全屏 25 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 26 WindowManager.LayoutParams.FLAG_FULLSCREEN); 27 setContentView(R.layout.activity_main); 28 WebView runWebView=null; 29 runWebView = (WebView) findViewById(R.id.runWebView); 30 runWebView.loadDataWithBaseURL(null,"<HTML><body bgcolor='#f3f3f3'><div align=center><IMG src='file:///android_asset/+-.gif'/></div></body></html>", "text/html", "UTF-8",null); 31 32 SysApplication.getInstance().addActivity(this); 33 //延时换屏 34 final Intent intent=new Intent(); 35 Timer timer=new Timer(); 36 TimerTask task = new TimerTask() { 37 @Override 38 public void run() { 39 intent.setClass(MainActivity.this, homeactivity.class); 40 startActivity(intent); 41 } 42 }; 43 timer.schedule(task, 7000); //延时7秒 44 } 45 46 47 48 49 50 51 52 }
Culculter.java
1 package com.team.randomcal; 2 3 import java.util.*; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.Gravity; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.*; 11 12 13 public class Calculter extends Activity{ 14 private TextView textView1; //标题 15 private TextView textView2; //输入题目数 16 private TextView yun1; //运算数1 17 private TextView yun2; //运算数2 18 private TextView symbol; //运算符 19 private TextView num; //出题数 20 21 private Button start; //开始按钮 22 private Button check; //核对答案 23 private Button next; //下一道题 24 private Button again; //再来一次 25 26 private EditText answer; //答案 27 private EditText shumu; //数目 28 29 private int value1; 30 private int value2; 31 private int result; //结果 32 private int j=1; //设置题目序号 33 private int rightnum=0; //判断正确的题目数 34 private int errornum=0; //判断错误的题目数 35 36 protected void onCreate(Bundle savedInstanceState) 37 { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.callayout); //进入出题界面 40 InitButton(); //初始化控件 41 42 //开始按钮的点击事件监听 43 start.setOnClickListener 44 ( 45 new OnClickListener() 46 { 47 public void onClick(View v) 48 { 49 Toast toast; //提醒 50 //判断输入题目为空或零时 51 if(shumu.getText().toString().equals("")==true||Integer.parseInt(shumu.getText().toString())==0) 52 { 53 toast=Toast.makeText(getApplicationContext(), "请输入题目数量", Toast.LENGTH_SHORT); 54 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 55 toast.show(); 56 } 57 else 58 { 59 dotitle(); //生成题目 60 } 61 } 62 } 63 ); 64 65 //检查按钮 66 check.setOnClickListener 67 ( 68 new OnClickListener() { 69 70 @Override 71 public void onClick(View v) { 72 // TODO Auto-generated method stub 73 74 Toast toast; //提醒 75 //判断输入答案为空时 76 if(answer.getEditableText().toString().equals("")==true) 77 { 78 toast=Toast.makeText(getApplicationContext(), "请输入答案", Toast.LENGTH_SHORT); 79 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 80 toast.show(); 81 } 82 else 83 { 84 value1=Integer.parseInt(yun1.getText().toString()); 85 value2=Integer.parseInt(yun2.getText().toString()); 86 87 //加法判断 88 if(symbol.getText().toString().equals("+")==true) 89 { 90 result=value1+value2; 91 int number1=Integer.parseInt(answer.getText().toString()); 92 //答案判断 93 if(result==number1) 94 { 95 toast=Toast.makeText(getApplicationContext(), "恭喜你答对了!", Toast.LENGTH_SHORT); 96 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 97 toast.show(); 98 rightnum++; 99 } 100 else 101 { 102 toast=Toast.makeText(getApplicationContext(), "抱歉你答错了!"+" "+"答案是"+result, Toast.LENGTH_SHORT); 103 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 104 toast.show(); 105 errornum++; 106 } 107 } 108 //减法判断 109 if(symbol.getText().toString().equals("-")==true) 110 { 111 result=value1-value2; 112 int number1=Integer.parseInt(answer.getText().toString()); 113 //答案判断(为负数重新生成) 114 if(result<0) 115 { 116 toast=Toast.makeText(getApplicationContext(), "答案为负请按下一题", Toast.LENGTH_SHORT); 117 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 118 toast.show(); 119 j--; 120 } 121 else 122 { 123 if(result==number1) 124 { 125 toast=Toast.makeText(getApplicationContext(), "恭喜你答对了!", Toast.LENGTH_SHORT); 126 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 127 toast.show(); 128 rightnum++; 129 } 130 else 131 { 132 toast=Toast.makeText(getApplicationContext(), "抱歉你答错了!"+" "+"答案是"+result, Toast.LENGTH_SHORT); 133 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 134 toast.show(); 135 errornum++; 136 } 137 } 138 } 139 140 //乘法判断 141 //加法判断 142 if(symbol.getText().toString().equals("*")==true) 143 { 144 result=value1*value2; 145 int number1=Integer.parseInt(answer.getText().toString()); 146 //答案判断 147 if(result==number1) 148 { 149 toast=Toast.makeText(getApplicationContext(), "恭喜你答对了!", Toast.LENGTH_SHORT); 150 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 151 toast.show(); 152 rightnum++; 153 } 154 else 155 { 156 toast=Toast.makeText(getApplicationContext(), "抱歉你答错了!"+" "+"答案是"+result, Toast.LENGTH_SHORT); 157 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 158 toast.show(); 159 errornum++; 160 } 161 } 162 //除法判断 163 //加法判断 164 if(symbol.getText().toString().equals("/")==true) 165 { 166 result=value1/value2; 167 int number1=Integer.parseInt(answer.getText().toString()); 168 //答案判断 169 if(result==number1) 170 { 171 toast=Toast.makeText(getApplicationContext(), "恭喜你答对了!", Toast.LENGTH_SHORT); 172 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 173 toast.show(); 174 rightnum++; 175 } 176 else 177 { 178 toast=Toast.makeText(getApplicationContext(), "抱歉你答错了!"+" "+"答案是"+result, Toast.LENGTH_SHORT); 179 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 180 toast.show(); 181 errornum++; 182 } 183 } 184 } 185 } 186 187 } 188 ); 189 190 //下一题点击事件监听 191 next.setOnClickListener 192 ( 193 new OnClickListener() { 194 195 @Override 196 public void onClick(View v) { 197 // TODO Auto-generated method stub 198 199 Toast toast; 200 //判断是否写出题目的答案 201 if(answer.getEditableText().toString().equals("")==true) 202 { 203 toast=Toast.makeText(getApplicationContext(), "请输入答案!", Toast.LENGTH_SHORT); 204 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 205 toast.show(); 206 } 207 else 208 { 209 if(j==Integer.parseInt(shumu.getText().toString())) 210 { 211 toast=Toast.makeText(getApplicationContext(), "您已经完成本次测试!"+" "+"你共答对"+rightnum+"题 答错"+errornum+"题", Toast.LENGTH_SHORT); 212 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 213 toast.show(); 214 } 215 else 216 { 217 yun1.setText(""); 218 yun2.setText(""); 219 symbol.setText(""); 220 answer.setText(""); //清空答案 221 num.setText(""); 222 j++; 223 dotitle(); 224 225 } 226 } 227 } 228 } 229 ); 230 231 //again按钮的事件监听 232 again.setOnClickListener 233 ( 234 new OnClickListener() { 235 236 @Override 237 public void onClick(View arg0) { 238 // TODO Auto-generated method stub 239 Toast toast; 240 241 toast=Toast.makeText(getApplicationContext(), "请开始你的答题!", Toast.LENGTH_SHORT); 242 toast.setGravity(Gravity.CENTER, 0, 0); //Toast 居中显示 243 toast.show(); 244 //清空原有的值 245 shumu.setText(""); 246 yun1.setText(""); 247 yun2.setText(""); 248 symbol.setText(""); 249 answer.setText(""); //清空答案 250 num.setText(""); 251 rightnum=0; 252 errornum=0; 253 j=1; 254 } 255 } 256 ); 257 } 258 259 //初始化控件 260 public void InitButton() 261 { 262 //从布局中获取控件 263 textView1=(TextView)findViewById(R.id.count_text1); 264 textView2=(TextView)findViewById(R.id.count_text2); 265 yun1=(TextView)findViewById(R.id.yun1); 266 yun2=(TextView)findViewById(R.id.yun2); 267 symbol=(TextView)findViewById(R.id.symbol); 268 num=(TextView)findViewById(R.id.num); //第几题 269 270 shumu=(EditText)findViewById(R.id.shumu); 271 answer=(EditText)findViewById(R.id.answer); 272 273 start=(Button)findViewById(R.id.start); 274 next=(Button)findViewById(R.id.next); 275 check=(Button)findViewById(R.id.check); 276 again=(Button)findViewById(R.id.again); 277 } 278 279 //增加按钮监听否则程序会意外退出 280 public void buttonListener(Button button,final int id) 281 { 282 button.setOnClickListener 283 ( 284 new OnClickListener() { 285 286 @Override 287 public void onClick(View v) { 288 // TODO Auto-generated method stub 289 yun1.setText(""); 290 yun2.setText(""); 291 symbol.setText(""); 292 num.setText(""); 293 answer.setText(""); 294 } 295 } 296 ); 297 } 298 //出题函数 299 public void dotitle() 300 { 301 String a=""; 302 String b=""; 303 304 int k=0,p=30; //0-30 305 //生成0-30的两个随机数 306 a=String.valueOf((int)(Math.random()*p+k)); 307 b=String.valueOf((int)(Math.random()*p+k)); 308 String []op={"+","-","*","/"}; 309 Random r=new Random(); //默认当前系统时间的毫秒数作为种子数 310 int d=r.nextInt(4); //随机数的上限为4(不包括) 311 312 //加入判断(除法除数为0 重新生成除数) 313 while(b=="0") 314 { 315 b=String.valueOf((int)(Math.random()*p+k)); 316 } 317 318 yun1.setText(a); 319 yun2.setText(b); 320 symbol.setText(op[d]); 321 num.setText(String.valueOf(j)); 322 } 323 324 }
countActivity.java
package com.team.randomcal; import java.util.Random; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class countActivity extends Activity implements OnClickListener{ //定义按钮 private TextView counttext1,counttext2; private Button ResultBtn; private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13; private int result=0; private String op_fen; private int sum_title = 0, true_title = 0, flase_title = 0;//判断题目正确数 private String []op_fu={"+","-","x","÷"}; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.countlayout); SysApplication.getInstance().addActivity(this); InitButton(); //初始化按钮 //注册按钮监听 b0.setOnClickListener(this); b1.setOnClickListener(this); b2.setOnClickListener(this); b3.setOnClickListener(this); b4.setOnClickListener(this); b5.setOnClickListener(this); b6.setOnClickListener(this); b7.setOnClickListener(this); b8.setOnClickListener(this); b9.setOnClickListener(this); b10.setOnClickListener(this); b11.setOnClickListener(this); b12.setOnClickListener(this); b13.setOnClickListener(this); //获得传递性息 Intent intent=getIntent(); final String info=intent.getStringExtra("info"); //出题 if(info.equals("easy")) { dotitle1(); } else if(info.equals("advance")) { dotitle2(); } else if(info.equals("raise")) { dotitle3(); } else { dotitle4(); } //结果判断 ResultBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast toast; if(op_fen .equals(counttext2.getText().toString())) { true_title++; sum_title++; //带图片的toast toast=Toast.makeText(countActivity.this, "恭喜你答对了!", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0);// 中间显示 LinearLayout toastLayout = (LinearLayout) toast.getView(); ImageView imageView = new ImageView(getApplicationContext()); imageView.setImageResource(R.drawable.xiao); toastLayout.addView(imageView, 0);// 0 图片在文字的上方 toast.show(); } else { flase_title++; sum_title++; toast=Toast.makeText(countActivity.this, "答错了!答案是:"+op_fen, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0);// 中间显示 LinearLayout toastLayout = (LinearLayout) toast.getView(); ImageView imageView = new ImageView(getApplicationContext()); imageView.setImageResource(R.drawable.ku); toastLayout.addView(imageView, 0);// 0 图片在文字的上方 toast.show(); } //清空 counttext2.setText(""); //再次出题 if(info.equals("easy")) { dotitle1(); } else if(info.equals("advance")) { dotitle2(); } else if(info.equals("raise")) { dotitle3(); } else { Random r=new Random(); int j=r.nextInt(2); if(j==0) dotitle3(); else dotitle4(); } } } ); } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.count,menu); return true; } //响应菜单事件 public boolean onOptionsItemSelected(MenuItem item) { Toast toast; switch(item.getItemId()) { case R.id.count_item1: toast=Toast.makeText(this, "暂无可用信息", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0);// 中间显示 toast.show(); break; case R.id.count_item2: toast=Toast.makeText(this, "您答对"+true_title+"道题,答错"+flase_title+"道题,正确率"+Rate()+"%", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0);// 中间显示 toast.show(); break; default: toast=Toast.makeText(this, "下次再接再厉", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0);// 中间显示 toast.show(); finish(); //退出本次活动和back键一样 break; } return super.onOptionsItemSelected(item); } //计算正确率 public double Rate() { return Math.round(((double)true_title/sum_title)*100); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.b0: counttext2.append("0"); break; case R.id.b1: counttext2.append("1"); break; case R.id.b2: counttext2.append("2"); break; case R.id.b3: counttext2.append("3"); break; case R.id.b4: counttext2.append("4"); break; case R.id.b5: counttext2.append("5"); break; case R.id.b6: counttext2.append("6"); break; case R.id.b7: counttext2.append("7"); break; case R.id.b8: counttext2.append("8"); break; case R.id.b9: counttext2.append("9"); break; case R.id.b10: counttext2.setText(""); //清除 break; case R.id.b11: counttext2.append("."); break; case R.id.b12: counttext2.append("-"); //负号 break; case R.id.b13: counttext2.append("/"); //分号 break; } } //初始化控件 public void InitButton() { counttext1=(TextView)findViewById(R.id.count_text1); counttext2=(TextView)findViewById(R.id.count_text2); ResultBtn=(Button)findViewById(R.id.b15); b0=(Button)findViewById(R.id.b0); b1=(Button)findViewById(R.id.b1); b2=(Button)findViewById(R.id.b2); b3=(Button)findViewById(R.id.b3); b4=(Button)findViewById(R.id.b4); b5=(Button)findViewById(R.id.b5); //5 b6=(Button)findViewById(R.id.b6); b7=(Button)findViewById(R.id.b7); b8=(Button)findViewById(R.id.b8); b9=(Button)findViewById(R.id.b9); b10=(Button)findViewById(R.id.b10); b11=(Button)findViewById(R.id.b11); b12=(Button)findViewById(R.id.b12); b13=(Button)findViewById(R.id.b13); } //出题 public void dotitle1() //只做加减 { int num1=0; int num2=0; //String op_yun; //保存表达式 Random r=new Random(); //默认当前系统时间的毫秒数作为种子数 int sign=r.nextInt(4); //符号随机数的上限为4(不包括) num1=(int)(1+Math.random()*30); //0-30随机数 num2=(int)(1+Math.random()*30); if(sign % 2 == 0){ //加法 counttext1.setText(num1+"+"+num2); result = num1+num2; op_fen=String.valueOf(result); }else{ //减法 counttext1.setText(num1+"-"+num2); result = num1-num2; op_fen=String.valueOf(result); } } public void dotitle2() //加减乘除 { int num1=0; int num2=0; //String op_yun; //保存表达式 num1=(int)(1+Math.random()*30); //0-30随机数 num2=(int)(1+Math.random()*30); int g=0; //公约数 Random r=new Random(); //默认当前系统时间的毫秒数作为种子数 int sign=r.nextInt(4); //符号随机数的上限为4(不包括) if(sign == 0){ //加法 counttext1.setText(num1+"+"+num2); result = num1+num2; op_fen=String.valueOf(result); }else if(sign == 1){ //减法 counttext1.setText(num1+"-"+num2); result = num1-num2; op_fen=String.valueOf(result); } else if(sign == 2){ //乘法 counttext1.setText(num1+"x"+num2); result = num1*num2; op_fen=String.valueOf(result); } else{ //除法 counttext1.setText(num1+"÷"+num2); //除不尽 化为分数 if((num1%num2)!=0) { g=div_cl(num1, num2); fen_cl(num1,num2,g); } else { result = num1/num2; op_fen=String.valueOf(result); } } } public void dotitle3() //分数加减实现 { int z_num1=0; //第一个数分子 z_num1=(int)(1+Math.random()*30); //0-30随机数 int m_num1=0; //第一个数分母 m_num1=(int)(1+Math.random()*30); //0-30随机数 int z_num2=0; //第二个数分子 z_num2=(int)(1+Math.random()*30); int m_num2=0; //第二个数分母 m_num2=(int)(1+Math.random()*30); Random r=new Random(); //默认当前系统时间的毫秒数作为种子数 int sign=r.nextInt(4); //符号随机数的上限为4(不包括) int sum_z=0; int sum_m=0; int g=0; //公约数 if(sign == 0){ //分数加法 counttext1.setText(z_num1+"/"+m_num1+"+"+z_num2+"/"+m_num2); sum_z=z_num1*m_num2+z_num2*m_num1; sum_m=m_num1*m_num2; //求分子分母最大公约数 g=div_cl(sum_z,sum_m); //化简为最简真分数 fen_cl(sum_z,sum_m,g); } else if(sign == 1) //减法 { counttext1.setText(z_num1+"/"+m_num1+"-"+z_num2+"/"+m_num2); sum_z=(z_num1*m_num2)-(z_num2*m_num1); while(sum_z<0) { //重新生成 z_num1=(int)(1+Math.random()*30); //0-30随机数 m_num1=(int)(1+Math.random()*30); //0-30随机数 z_num2=(int)(1+Math.random()*30); m_num2=(int)(1+Math.random()*30); sum_z=(z_num1*m_num2)-(z_num2*m_num1); } sum_m=m_num1*m_num2; g=div_cl(sum_z, sum_m); fen_cl(sum_z, sum_m, g); } else if(sign == 2) //乘法 { counttext1.setText(z_num1+"/"+m_num1+"x"+z_num2+"/"+m_num2); sum_z=z_num1*z_num2; sum_m=m_num2*m_num1; g=div_cl(sum_z, sum_m); fen_cl(sum_z, sum_m, g); } else //除法 { counttext1.setText(z_num1+"/"+m_num1+"÷"+z_num2+"/"+m_num2); sum_z=z_num1*m_num2; sum_m=z_num2*m_num1; g=div_cl(sum_z, sum_m); fen_cl(sum_z, sum_m, g); } } //求公约数 public int div_cl(int x,int y) { int t; while(y!=0) { t=x%y; x=y; y=t; } return x; } //分数表示 public void fen_cl(int a,int b,int g) { String s_z,s_m; if(g==1) { if(a!=b) { s_z=String.valueOf(a); s_m=String.valueOf(b); op_fen=s_z+"/"+s_m; //表达式 } else { s_z=String.valueOf(1); op_fen=s_z; } } else { int x=a/g; int y=b/g; if(y==1) { s_z=String.valueOf(x); op_fen=s_z; } else { s_z=String.valueOf(x); s_m=String.valueOf(y); op_fen=s_z+"/"+s_m; //表达式 } } } public void dotitle4() { //产生括号的计算式(三个数) int num1=0; int num2=0; int num3=0; int ch=0; //情况选择 //String op_yun; //保存表达式 num1=(int)(1+Math.random()*30); //0-30随机数 num2=(int)(1+Math.random()*30); num3=(int)(1+Math.random()*30); int g=0; //公约数 Random r=new Random(); //默认当前系统时间的毫秒数作为种子数 int sign1=r.nextInt(4); //符号随机数的上限为4(不包括) ch=r.nextInt(2); //判断是否有括号 int sign2=r.nextInt(4); if(ch==1) { counttext1.setText("("+num1+OP(sign1)+num2+")"+OP(sign2)+num3); if(OP(sign1)!="÷") { if(OP(sign2)!="÷") { result=yunsuan1((yunsuan1(num1,num2,sign1)),num3,sign2); op_fen=String.valueOf(result); } else { int x=yunsuan1(num1,num2,sign1); g=div_cl(x,num3); fen_cl(x,num3,g); } } else { //看成两个分数的运算 g=div_cl(num1,num2); yunsuan2(num1,num2,num3,1,sign2,g); } } else { counttext1.setText(num1+OP(sign1)+"("+num2+OP(sign2)+num3+")"); if(OP(sign2)!="÷") { if(OP(sign1)!="÷") { result=yunsuan1(num1,(yunsuan1(num2,num3,sign2)),sign1); op_fen=String.valueOf(result); } else { int x=yunsuan1(num2,num3,sign1); g=div_cl(num1,x); fen_cl(num1,x,g); } } else { //看成两个分数的运算 g=div_cl(num2,num3); yunsuan2(num1,1,num2,num3,sign1,g); } } } public String OP(int k) { return op_fu[k]; } //运算 public void yunsuan2(int z_num1,int m_num1,int z_num2,int m_num2,int sign ,int g) { int sum_z=0; int sum_m=0; if(sign == 0){ //分数加法 sum_z=z_num1*m_num2+z_num2*m_num1; sum_m=m_num1*m_num2; //求分子分母最大公约数 g=div_cl(sum_z,sum_m); //化简为最简真分数 fen_cl(sum_z,sum_m,g); } else if(sign == 1) //减法 { sum_z=(z_num1*m_num2)-(z_num2*m_num1); while(sum_z<0) { //重新生成 z_num1=(int)(1+Math.random()*30); //0-30随机数 m_num1=(int)(1+Math.random()*30); //0-30随机数 z_num2=(int)(1+Math.random()*30); m_num2=(int)(1+Math.random()*30); sum_z=(z_num1*m_num2)-(z_num2*m_num1); } sum_m=m_num1*m_num2; g=div_cl(sum_z, sum_m); fen_cl(sum_z, sum_m, g); } else if(sign == 2) //乘法 { sum_z=z_num1*z_num2; sum_m=m_num2*m_num1; g=div_cl(sum_z, sum_m); fen_cl(sum_z, sum_m, g); } else //除法 { sum_z=z_num1*m_num2; sum_m=z_num2*m_num1; g=div_cl(sum_z, sum_m); fen_cl(sum_z, sum_m, g); } } public int yunsuan1(int num1,int num2,int sign) { if(sign == 0){ //加法 return num1+num2; }else if(sign == 1){ //减法 return num1*num2; } else if(sign == 2){ //乘法 return num1*num2; } else{ //除法 if(num1%num2==0) return num1/num2; else { return 0xffff; } } } }
homeactivity.java
1 package com.team.randomcal; 2 3 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 11 public class homeactivity extends Activity { 12 13 private Button startbtn,aboutbtn,exitbtn; 14 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.home_footbar_layout); 18 19 SysApplication.getInstance().addActivity(this); 20 InitButton(); //初始化按钮 21 22 //开始游戏 23 startbtn.setOnClickListener(new OnClickListener() { 24 @Override 25 public void onClick(View arg0) { 26 // TODO Auto-generated method stub 27 Intent intent = new Intent(); 28 intent.setClass(homeactivity.this, selectActivity.class); 29 startActivity(intent); 30 } 31 }); 32 33 //关于信息 34 35 //退出游戏 36 exitbtn.setOnClickListener(new OnClickListener() { 37 @Override 38 public void onClick(View arg0) { 39 // TODO Auto-generated method stub 40 41 //退出整个应用程序 42 SysApplication.getInstance().exit(); 43 } 44 }); 45 } 46 47 public void InitButton() 48 { 49 startbtn=(Button)findViewById(R.id.start_app); 50 aboutbtn=(Button)findViewById(R.id.about_game); 51 exitbtn=(Button)findViewById(R.id.exit_app); 52 53 } 54 }
selectActivity.java
1 package com.team.randomcal; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.*; 8 import android.widget.Button; 9 10 public class selectActivity extends Activity { 11 12 private Button simplebtn,adbtn,rabtn,diffbtn; 13 protected void onCreate(Bundle savedInstanceState) 14 { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.select_layout); 17 18 SysApplication.getInstance().addActivity(this); 19 InitButton(); //初始化按钮 20 21 //入门按钮事件 22 simplebtn.setOnClickListener(new OnClickListener() 23 { 24 25 @Override 26 public void onClick(View v) { 27 // TODO Auto-generated method stub 28 //向下一个传递参数 29 String data="easy"; 30 Intent intent=new Intent(); 31 intent.setClass(selectActivity.this, countActivity.class); 32 intent.putExtra("info", data); 33 startActivity(intent); 34 } 35 } 36 ); 37 38 //进阶按钮 39 adbtn.setOnClickListener(new OnClickListener() 40 { 41 42 @Override 43 public void onClick(View v) { 44 // TODO Auto-generated method stub 45 //向下一个传递参数 46 String data="advance"; 47 Intent intent=new Intent(); 48 intent.setClass(selectActivity.this, countActivity.class); 49 intent.putExtra("info", data); 50 startActivity(intent); 51 } 52 } 53 ); 54 55 //提升按钮 56 rabtn.setOnClickListener(new OnClickListener() 57 { 58 59 @Override 60 public void onClick(View v) { 61 // TODO Auto-generated method stub 62 //向下一个传递参数 63 String data="raise"; 64 Intent intent=new Intent(); 65 intent.setClass(selectActivity.this, countActivity.class); 66 intent.putExtra("info", data); 67 startActivity(intent); 68 } 69 } 70 ); 71 72 //困难按钮 73 diffbtn.setOnClickListener(new OnClickListener() 74 { 75 76 @Override 77 public void onClick(View v) { 78 // TODO Auto-generated method stub 79 //向下一个传递参数 80 String data="difficult"; 81 Intent intent=new Intent(); 82 intent.setClass(selectActivity.this, countActivity.class); 83 intent.putExtra("info", data); 84 startActivity(intent); 85 } 86 } 87 ); 88 } 89 90 //初始化按钮 91 public void InitButton() 92 { 93 simplebtn=(Button)findViewById(R.id.easy); 94 adbtn=(Button)findViewById(R.id.advance); 95 rabtn=(Button)findViewById(R.id.raise1); 96 diffbtn=(Button)findViewById(R.id.different); 97 } 98 }
SysApplication.java
1 package com.team.randomcal; 2 3 import java.util.LinkedList; 4 import java.util.List; 5 import android.app.Activity; 6 import android.app.AlertDialog; 7 import android.app.Application; 8 import android.content.DialogInterface; 9 import android.content.Intent; 10 11 public class SysApplication extends Application { 12 private List<Activity> mList = new LinkedList<Activity>(); 13 private static SysApplication instance; 14 15 private SysApplication() { 16 } 17 public synchronized static SysApplication getInstance() { 18 if (null == instance) { 19 instance = new SysApplication(); 20 } 21 return instance; 22 } 23 // add Activity 24 public void addActivity(Activity activity) { 25 mList.add(activity); 26 } 27 28 public void exit() { 29 try { 30 for (Activity activity : mList) { 31 if (activity != null) 32 activity.finish(); 33 } 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } finally { 37 System.exit(0); 38 } 39 } 40 public void onLowMemory() { 41 super.onLowMemory(); 42 System.gc(); 43 } 44 }
UI源代码:
activity_main.xml
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:background="#F3F3F3" 6 > 7 8 9 <WebView 10 android:id="@+id/runWebView" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_centerHorizontal="true" 14 android:layout_centerVertical="true" /> 15 16 <TextView 17 android:id="@+id/fullscreen_content" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_below="@+id/runWebView" 21 android:layout_centerHorizontal="true" 22 android:gravity="center" 23 android:keepScreenOn="true" 24 android:text="正在加载请等待..." 25 android:textColor="#33b5e5" 26 android:textSize="30sp" 27 android:textStyle="bold" /> 28 29 </RelativeLayout>
countlayout.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="147dp" 10 android:background="@drawable/bk0" 11 android:orientation="vertical" > 12 13 <LinearLayout 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" > 16 17 <TextView 18 android:id="@+id/count_text1" 19 android:layout_width="match_parent" 20 android:layout_height="73dp" 21 android:gravity="center_vertical" 22 android:textSize="33sp" /> 23 </LinearLayout> 24 25 <LinearLayout 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:layout_gravity="center_horizontal" > 29 30 <TextView 31 android:id="@+id/count_text2" 32 android:layout_width="match_parent" 33 android:layout_height="82dp" 34 android:layout_gravity="right|center_vertical" 35 android:textSize="40sp" /> 36 </LinearLayout> 37 </LinearLayout> 38 39 <LinearLayout 40 android:layout_width="match_parent" 41 android:layout_height="78dp" > 42 43 <Button 44 android:id="@+id/b1" 45 android:layout_width="0dp" 46 android:layout_height="fill_parent" 47 android:layout_weight="1.0" 48 android:layout_marginLeft="3dp" 49 android:background="@drawable/s1" 50 android:text="@string/one" 51 android:textSize="40sp" /> 52 53 <Button 54 android:id="@+id/b2" 55 android:layout_width="0dp" 56 android:layout_height="fill_parent" 57 android:layout_weight="1.0" 58 android:layout_marginLeft="5dp" 59 android:background="@drawable/s2" 60 android:text="@string/two" 61 android:textSize="40sp" /> 62 63 <Button 64 android:id="@+id/b3" 65 android:layout_width="0dp" 66 android:layout_height="fill_parent" 67 android:layout_weight="1.0" 68 android:layout_marginLeft="5dp" 69 android:layout_marginRight="2dp" 70 android:background="@drawable/s3" 71 android:text="@string/three" 72 android:textSize="40sp" /> 73 74 <Button 75 android:id="@+id/b10" 76 android:layout_width="0dp" 77 android:layout_height="fill_parent" 78 android:layout_marginLeft="5dp" 79 android:layout_weight="1.0" 80 android:background="@drawable/sc" 81 android:text="@string/back" 82 android:textSize="40sp" /> 83 84 </LinearLayout> 85 86 <LinearLayout 87 android:layout_width="match_parent" 88 android:layout_height="79dp" > 89 90 <Button 91 android:id="@+id/b4" 92 android:layout_width="0dp" 93 android:layout_height="fill_parent" 94 android:layout_weight="1.0" 95 android:background="@drawable/s4" 96 android:text="@string/four" 97 android:textSize="40sp" /> 98 99 <Button 100 android:id="@+id/b5" 101 android:layout_width="0dp" 102 android:layout_height="fill_parent" 103 android:layout_weight="1.0" 104 android:background="@drawable/s5" 105 android:text="@string/five" 106 android:textSize="40sp" /> 107 108 <Button 109 android:id="@+id/b6" 110 android:layout_width="0dp" 111 android:layout_height="fill_parent" 112 android:layout_weight="1.0" 113 android:background="@drawable/s6" 114 android:text="@string/six" 115 android:textSize="40sp" /> 116 117 <Button 118 android:id="@+id/b11" 119 android:layout_width="0dp" 120 android:layout_height="fill_parent" 121 android:layout_weight="1.0" 122 android:background="@drawable/sp" 123 android:text="@string/point" 124 android:textSize="40sp" /> 125 126 </LinearLayout> 127 128 <LinearLayout 129 android:layout_width="match_parent" 130 android:layout_height="78dp" > 131 132 <Button 133 android:id="@+id/b7" 134 android:layout_width="0dp" 135 android:layout_height="fill_parent" 136 android:layout_weight="1.0" 137 android:background="@drawable/s7" 138 android:text="@string/seven" 139 android:textSize="40sp" /> 140 141 <Button 142 android:id="@+id/b8" 143 android:layout_width="0dp" 144 android:layout_height="fill_parent" 145 android:layout_weight="1.0" 146 android:background="@drawable/s8" 147 android:text="@string/eight" 148 android:textSize="40sp" /> 149 150 <Button 151 android:id="@+id/b9" 152 android:layout_width="0dp" 153 android:layout_height="fill_parent" 154 android:layout_weight="1.0" 155 android:background="@drawable/s9" 156 android:text="@string/nine" 157 android:textSize="40sp" /> 158 159 <Button 160 android:id="@+id/b12" 161 android:layout_width="0dp" 162 android:layout_height="fill_parent" 163 android:layout_weight="1.0" 164 android:background="@drawable/sp" 165 android:text="@string/minus" 166 android:textSize="40sp" /> 167 168 </LinearLayout> 169 170 <LinearLayout 171 android:layout_width="match_parent" 172 android:layout_height="match_parent" > 173 174 <Button 175 android:id="@+id/b13" 176 android:layout_width="0dp" 177 android:layout_height="fill_parent" 178 android:layout_weight="1.0" 179 android:background="@drawable/sp" 180 android:text="@string/diagonal" 181 android:textSize="40sp" /> 182 183 <Button 184 android:id="@+id/b0" 185 android:layout_width="0dp" 186 android:layout_height="fill_parent" 187 android:layout_weight="1.0" 188 android:background="@drawable/s0" 189 android:text="@string/zero" 190 android:textSize="40sp" /> 191 192 <Button 193 android:id="@+id/b15" 194 android:layout_width="0dp" 195 android:layout_height="fill_parent" 196 android:layout_weight="1.0" 197 android:background="@drawable/sf" 198 android:text="@string/affirm" 199 android:textSize="40sp" /> 200 </LinearLayout> 201 202 </LinearLayout>
home_footbar_layout.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/LinearLayout1" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:orientation="vertical" > 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="50dp" 11 android:background="@drawable/bottom_bg" > 12 13 <LinearLayout 14 android:id="@+id/bottom_home_layout_ly" 15 android:layout_width="64dip" 16 android:layout_height="match_parent" 17 android:layout_marginTop="4dip" 18 android:layout_weight="1" 19 android:background="@drawable/home_bottom_style" 20 android:orientation="vertical" > 21 22 <ImageView 23 android:id="@+id/home_bottom_home_bn" 24 android:layout_width="28dip" 25 android:layout_height="28dip" 26 android:layout_gravity="center" 27 android:background="@drawable/bottom_home_bn_style" /> 28 29 <TextView 30 android:id="@+id/home_bottom_home_tv" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:layout_gravity="center" 34 android:gravity="center_horizontal" 35 android:paddingTop="2dip" 36 android:text="@string/home_tv" 37 android:textColor="@android:color/white" 38 android:textSize="10dip" 39 android:textStyle="bold" /> 40 </LinearLayout> 41 42 1 43 44 <LinearLayout 45 android:id="@+id/bottom_cam_layout_ly" 46 android:layout_width="64dip" 47 android:layout_height="match_parent" 48 android:layout_marginTop="4dip" 49 android:layout_weight="1" 50 android:background="@drawable/home_bottom_style" 51 android:orientation="vertical" > 52 53 <ImageView 54 android:id="@+id/home_bottom_cam_bn" 55 android:layout_width="43dip" 56 android:layout_height="33dip" 57 android:layout_gravity="center" 58 android:layout_marginTop="5dip" 59 android:background="@drawable/bottom_cam_bn_style" /> 60 </LinearLayout> 61 62 <LinearLayout 63 android:id="@+id/bottom_shopping_layout_ly" 64 android:layout_width="64dip" 65 android:layout_height="match_parent" 66 android:layout_marginTop="4dip" 67 android:layout_weight="1" 68 android:background="@drawable/home_bottom_style" 69 android:orientation="vertical" > 70 71 <ImageView 72 android:id="@+id/home_bottom_shopping_bn" 73 android:layout_width="28dip" 74 android:layout_height="30dip" 75 android:layout_gravity="center" 76 android:background="@drawable/bottom_shopping_bn_style" /> 77 78 <TextView 79 android:id="@+id/home_bottom_shopping_tv" 80 android:layout_width="wrap_content" 81 android:layout_height="wrap_content" 82 android:layout_gravity="center" 83 android:gravity="center_horizontal" 84 android:paddingTop="2dip" 85 android:text="@string/shopping_tv" 86 android:textColor="@android:color/white" 87 android:textSize="10dip" 88 android:textStyle="bold" /> 89 </LinearLayout> 90 91 <LinearLayout 92 android:id="@+id/bottom_show_layout_ly" 93 android:layout_width="64dip" 94 android:layout_height="match_parent" 95 android:layout_marginTop="4dip" 96 android:layout_weight="1" 97 android:background="@drawable/home_bottom_style" 98 android:orientation="vertical" > 99 100 <ImageView 101 android:id="@+id/home_bottom_show_bn" 102 android:layout_width="28dip" 103 android:layout_height="30dip" 104 android:layout_gravity="center" 105 android:background="@drawable/bottom_show_bn_style" /> 106 107 <TextView 108 android:id="@+id/home_bottom_show_tv" 109 android:layout_width="wrap_content" 110 android:layout_height="wrap_content" 111 android:layout_gravity="center" 112 android:paddingTop="2dip" 113 android:text="@string/show_tv" 114 android:textColor="@android:color/white" 115 android:textSize="10dip" 116 android:textStyle="bold" /> 117 </LinearLayout> 118 </LinearLayout> 119 120 <RelativeLayout 121 android:id="@+id/RelativeLayout1" 122 android:layout_width="match_parent" 123 android:layout_height="match_parent" 124 android:layout_marginTop="0dp" 125 android:background="@drawable/g122" 126 android:orientation="vertical" > 127 128 <Button 129 android:id="@+id/start_app" 130 android:layout_width="200dp" 131 android:layout_height="60dp" 132 android:layout_alignParentLeft="true" 133 android:layout_alignParentTop="true" 134 android:layout_marginLeft="60dp" 135 android:layout_marginTop="50dp" 136 android:background="@drawable/y1" 137 android:paddingLeft="50dp" 138 android:text="@string/start_game" 139 android:textSize="28sp" /> 140 141 <Button 142 android:id="@+id/about_game" 143 android:layout_width="200dp" 144 android:layout_height="60dp" 145 android:layout_alignParentLeft="true" 146 android:layout_below="@+id/start_app" 147 android:layout_marginLeft="60dp" 148 android:layout_marginTop="50dp" 149 android:background="@drawable/y1" 150 android:paddingLeft="50dp" 151 android:text="@string/about" 152 android:textSize="28sp" /> 153 154 <Button 155 android:id="@+id/exit_app" 156 android:layout_width="200dp" 157 android:layout_height="60dp" 158 android:layout_alignParentLeft="true" 159 android:layout_below="@+id/about_game" 160 android:layout_marginLeft="60dp" 161 android:layout_marginTop="50dp" 162 android:background="@drawable/y1" 163 android:paddingLeft="50dp" 164 android:text="@string/exit_game" 165 android:textSize="28sp" /> 166 167 <ImageView 168 android:id="@+id/imageView2" 169 android:layout_width="50dp" 170 android:layout_height="50dp" 171 android:layout_alignBottom="@+id/about_game" 172 android:layout_alignLeft="@+id/about_game" 173 android:layout_alignRight="@+id/imageView1" 174 android:layout_alignTop="@+id/about_game" 175 android:src="@drawable/g10" /> 176 177 <ImageView 178 android:id="@+id/imageView1" 179 android:layout_width="60dp" 180 android:layout_height="60dp" 181 android:layout_alignLeft="@+id/start_app" 182 android:layout_alignTop="@+id/start_app" 183 android:paddingLeft="5dp" 184 android:src="@drawable/g0" /> 185 186 <ImageView 187 android:id="@+id/imageView3" 188 android:layout_width="50dp" 189 android:layout_height="50dp" 190 android:layout_alignBottom="@+id/exit_app" 191 android:layout_alignLeft="@+id/exit_app" 192 android:layout_alignRight="@+id/imageView2" 193 android:layout_alignTop="@+id/exit_app" 194 android:src="@drawable/s20" /> 195 196 </RelativeLayout> 197 198 </LinearLayout>
select_layout.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/LinearLayout1" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="@drawable/b2" 7 android:orientation="vertical" > 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="70dp" 12 android:layout_marginTop="50dp" > 13 14 <Button 15 android:id="@+id/easy" 16 android:layout_width="200dp" 17 android:layout_height="fill_parent" 18 android:layout_marginLeft="60dp" 19 android:background="@drawable/y1" 20 android:text="@string/simple" 21 android:textSize="40sp" /> 22 23 </LinearLayout> 24 25 <LinearLayout 26 android:layout_width="match_parent" 27 android:layout_height="70dp" 28 android:layout_marginTop="20dp" > 29 30 <Button 31 android:id="@+id/advance" 32 android:layout_width="200dp" 33 android:layout_height="fill_parent" 34 android:layout_marginLeft="60dp" 35 android:background="@drawable/y1" 36 android:text="@string/general" 37 android:textSize="40sp" /> 38 39 </LinearLayout> 40 41 <LinearLayout 42 android:layout_width="match_parent" 43 android:layout_height="70dp" 44 android:layout_marginTop="20dp" > 45 46 <Button 47 android:id="@+id/raise1" 48 android:layout_width="200dp" 49 android:layout_height="fill_parent" 50 android:layout_marginLeft="60dp" 51 android:background="@drawable/y1" 52 android:text="@string/more_difficult" 53 android:textSize="40sp" /> 54 55 </LinearLayout> 56 57 <LinearLayout 58 android:layout_width="match_parent" 59 android:layout_height="70dp" 60 android:layout_marginTop="20dp" > 61 62 <Button 63 android:id="@+id/different" 64 android:layout_width="200dp" 65 android:layout_height="fill_parent" 66 android:layout_marginLeft="60dp" 67 android:background="@drawable/y1" 68 android:text="@string/difficult" 69 android:textSize="40sp" /> 70 71 </LinearLayout> 72 73 </LinearLayout>