Mainactivity:
public class MainActivity extends Activity {
Button start,end,wa;
int yourChose=-1;
private void showSinChosDia() {
final String[] mList={"普通","困难","加法练习","减法练习","乘法练习","除法练习"};
yourChose=-1;
AlertDialog.Builder sinChosDia=new AlertDialog.Builder(MainActivity.this);
sinChosDia.setTitle("难度/单项练习");
sinChosDia.setSingleChoiceItems(mList, 0, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
yourChose=which;
}
});
sinChosDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(yourChose==0)
{
Intent intent = new Intent(MainActivity.this, CalActivity.class);
startActivity(intent);
}
if(yourChose==1)
{
Intent intent = new Intent(MainActivity.this, CalActivity1.class);
startActivity(intent);
}
if(yourChose==2)
{
Intent intent = new Intent(MainActivity.this, CalActivity2.class);
startActivity(intent);
}
if(yourChose==3)
{
Intent intent = new Intent(MainActivity.this, CalActivity3.class);
startActivity(intent);
}
if(yourChose==4)
{
Intent intent = new Intent(MainActivity.this, CalActivity4.class);
startActivity(intent);
}
if(yourChose==5)
{
Intent intent = new Intent(MainActivity.this, CalActivity5.class);
startActivity(intent);
}
}
});
sinChosDia.create().show();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DBHelper helper = new DBHelper(this);
final Cursor c = helper.query();
start=(Button) findViewById(R.id.button1);
start.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showSinChosDia();
}
});
wa=(Button) findViewById(R.id.button2);
wa.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!c.moveToNext())
{
helper.close();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false);
builder.setTitle("结束");
builder.setMessage("无错题");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.create().show();
}
else{
Intent intent = new Intent(MainActivity.this, ShowDB.class);
startActivity(intent);
}
}
});
end=(Button) findViewById(R.id.button3);
end.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
}
CalActivity:
public class CalActivity extends Activity {
RelativeLayout rl1;
ImageView im; int op;
int a; int b; int n=0;
int w=0; String r;
Double answer,respon;
TextView textview1,textview2;
EditText editText;
Button button;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cal_main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
rl1=(RelativeLayout)findViewById(R.id.RelativeLayout1);
im=(ImageView)findViewById(R.id.imageView1);
textview1=(TextView)findViewById(R.id.textView1);
textview2=(TextView)findViewById(R.id.textView2);
editText=(EditText)findViewById(R.id.EditText1);
a =new Random().nextInt(100);
b =new Random().nextInt(100);
op=new Random().nextInt(4);
switch(op)//分支语句,判断OP是哪种情况
{
case 0:
textview1.setText(a+"+"+b+"=");
answer=(double) (a+b);
break;
case 1:
textview1.setText(a+"-"+b+"=");
answer=(double) (a-b);
break;
case 2:
a =new Random().nextInt(10);
b =new Random().nextInt(10);
textview1.setText(a+"*"+b+"=");
answer=(double) (a*b);
break;
case 3:
a =new Random().nextInt(10);
b =new Random().nextInt(10);
while(b==0){
b =new Random().nextInt(10);
}
textview1.setText(a+"/"+b+"=");
BigDecimal x = new BigDecimal((double)a/b); //保留2位小数。
answer = x.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
break; }
button=(Button)findViewById(R.id.button4);
button.setOnClickListener(new Button.OnClickListener(){
@Override public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!checkInteger(editText.getText().toString())) {
AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity.this);
builder.setCancelable(false);
builder.setTitle("错误");
builder.setMessage("你输入的信息有错");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
} });
builder.create().show();
editText.setText("");
return;
}
respon=Double.parseDouble(editText.getText().toString());
r=textview1.getText().toString();
ContentValues values = new ContentValues();
values.put("problem", r);
values.put("answer", answer);
editText.setText("");
n++;
if(respon.equals(answer)) {
im.setImageResource(R.drawable.d);
textview2.setText("你答对了!");
} else{
im.setImageResource(R.drawable.c);
DBHelper helper = new DBHelper(getApplicationContext());
final Cursor c = helper.query();
helper.insert(values);
w++;
textview2.setText("你答错了!\n"+r+answer);
helper.close(); } if(n<5)
{ a =new Random().nextInt(100);
b =new Random().nextInt(100);
op=new Random().nextInt(4);
switch(op) {
case 0: textview1.setText(a+"+"+b+"=");
answer=(double) (a+b);
break; case 1: textview1.setText(a+"-"+b+"=");
answer=(double) (a-b);
break; case 2: a =new Random().nextInt(10);
b =new Random().nextInt(10);
textview1.setText(a+"*"+b+"=");
answer=(double) (a*b); break;
case 3: a =new Random().nextInt(10);
b =new Random().nextInt(10);
while(b==0){ b =new Random().nextInt(10);
}
textview1.setText(a+"/"+b+"=");
BigDecimal x = new BigDecimal((double)a/b);
answer = x.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); break; } }
else {
int right=n-w;
double rvate=(double)right/n*100;
AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity.this);
builder.setCancelable(false);
builder.setTitle("结束");
builder.setMessage("你答对了"+right+"题"+"\n"+"答错了"+w+"题"+"\n"+"正确率为"+rvate+"%"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent(CalActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent); } });
builder.create().show(); } } });
} public boolean checkInteger(String text) {
/* 当输入的文本去掉前后空格长度为0时返回false */
if(text.trim().length()==0){ return false;
} try{ Double.parseDouble(text);
}catch(Exception e){ /* 无法转换为整数时返回false */
return false; }
return true; } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub item.setChecked(true);
switch (item.getItemId()) { case android.R.id.home:
//创建启动MainActivity的Intent Intent intent1=new Intent(this,MainActivity.class);
//添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出 intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent1); break;
case R.id.item4:
item.setChecked(true);
rl1.setBackgroundDrawable(getResources().getDrawable(android.R.color.black));
break; case R.id.item5:
item.setChecked(true);
rl1.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg1));
break; case R.id.item6:
item.setChecked(true);
rl1.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg3));
break; case R.id.item7:
item.setChecked(true); textview1.setTextColor(Color.YELLOW);
textview2.setTextColor(Color.YELLOW);
editText.setTextColor(Color.YELLOW);
button.setTextColor(Color.YELLOW);
break; case R.id.item8: item.setChecked(true);
textview1.setTextColor(Color.BLUE);
textview2.setTextColor(Color.BLUE);
editText.setTextColor(Color.BLUE);
button.setTextColor(Color.BLUE);
break; case R.id.item9: item.setChecked(true);
textview1.setTextColor(Color.RED);
textview2.setTextColor(Color.RED);
editText.setTextColor(Color.RED);
button.setTextColor(Color.RED);
break; case R.id.item10: item.setChecked(true);
textview1.setTextColor(Color.BLACK);
textview2.setTextColor(Color.BLACK);
editText.setTextColor(Color.BLACK);
button.setTextColor(Color.BLACK);
break; case R.id.item11: item.setChecked(true);
textview1.setTextSize(10);
textview2.setTextSize(10);
editText.setTextSize(10); button.setTextSize(10);
break; case R.id.item12: item.setChecked(true);
textview1.setTextSize(20);
textview2.setTextSize(20); editText.setTextSize(20);
button.setTextSize(20);
break; case R.id.item13: item.setChecked(true);
textview1.setTextSize(30); textview2.setTextSize(30);
editText.setTextSize(30); button.setTextSize(30);
break;
default: break; }
return super.onOptionsItemSelected(item); }
}