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); }
}