View Code
1 package com.example.flexd15; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.view.animation.Animation; 9 import android.view.animation.AnimationUtils; 10 import android.widget.AnalogClock; 11 import android.widget.Button; 12 import android.widget.RadioButton; 13 import android.widget.RadioGroup; 14 import android.widget.RadioGroup.OnCheckedChangeListener; 15 import android.widget.TextView; 16 import android.widget.Toast; 17 18 /** 19 * 每次在RadioGroup中随机内定一个作为正确答案, 20 * 点选RadioButton 点Button提交 判断是否正确 21 */ 22 public class MainActivity extends Activity { 23 24 private RadioButton rb1,rb2,rb3; 25 private Button bt1,bt2; 26 private TextView tv1; 27 private RadioGroup rg1; 28 private boolean choice = false;//选择是否正确 29 private int correctChoice = -2;//正确答案的ID 30 private int intTimes = 0; 31 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 setContentView(R.layout.activity_main); 36 37 //widget 38 bt1 = (Button)findViewById(R.id.bt1); 39 bt2 = (Button)findViewById(R.id.bt2); 40 rg1 = (RadioGroup) findViewById(R.id.rg1); 41 tv1 = (TextView) findViewById(R.id.tv1); 42 43 //指定答案 44 getAnswer(); 45 46 //Listener 47 bt1.setOnClickListener(buttonListener); 48 bt2.setOnClickListener(buttonListener); 49 rg1.setOnCheckedChangeListener(radioGroupListener); 50 } 51 52 /** 53 * 生成答案方法 54 */ 55 private void getAnswer(){ 56 rb1 = (RadioButton)findViewById(R.id.rb1); 57 rb2 = (RadioButton)findViewById(R.id.rb2); 58 rb3 = (RadioButton)findViewById(R.id.rb3); 59 60 // 供选择的答案,即三个RadioButton的ID 61 int[] choiceArray = { rb1.getId(), rb2.getId(), rb3.getId() }; 62 // 随机产生一个答案 63 int random = (int) (Math.random() * 3);// [0,1) => [0,2.7...) => 0,1,2 64 correctChoice = choiceArray[random]; 65 } 66 67 /** 68 * RadioGroup选择事件,选择后判断正误 69 */ 70 private RadioGroup.OnCheckedChangeListener radioGroupListener = new OnCheckedChangeListener() { 71 72 @Override 73 public void onCheckedChanged(RadioGroup group, int checkedId) { 74 if(checkedId==correctChoice){ 75 choice = true; 76 }else{ 77 choice = false; 78 } 79 } 80 }; 81 82 /** 83 * Button事件 84 */ 85 private View.OnClickListener buttonListener = new OnClickListener() { 86 87 @Override 88 public void onClick(View v) { 89 90 if(bt1.getId()==v.getId()){ 91 //ButtonSubmit 92 if(choice){ 93 //answer correct 94 getAnswer();//init 95 Toast.makeText(MainActivity.this, "correct", Toast.LENGTH_LONG).show(); 96 choice = false; 97 rg1.clearCheck(); 98 }else{ 99 //answer wrong 100 Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show(); 101 rg1.clearCheck(); 102 Animation ani = AnimationUtils.loadAnimation(MainActivity.this, R.anim.shake); 103 v.startAnimation(ani); 104 } 105 }else if(bt1.getId()==v.getId()){ 106 //ButtonClear 107 getAnswer(); 108 choice = false; 109 rg1.clearCheck(); 110 } 111 } 112 }; 113 114 115 @Override 116 public boolean onCreateOptionsMenu(Menu menu) { 117 // Inflate the menu; this adds items to the action bar if it is present. 118 getMenuInflater().inflate(R.menu.activity_main, menu); 119 return true; 120 } 121 122 }