RadioButton 猜猜看实例

public class Ex_Ctrl_6 extends Activity implements Button.OnClickListener {
/** Called when the activity is first created. */
private TextView answer_TextView;
private RadioButton boy_RadioButton, girl_RadioButton;
private RadioGroup radioGroup;
private Button answer_Button, clean_Button;
private String[] boy_girl;
private AlertDialog.Builder showRightorNot;
private MenuItem exit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 结果显示对话框 */
showRightorNot = new AlertDialog.Builder(this);
/* findViewById()从XML中获取资源对象 */
answer_TextView = (TextView) findViewById(R.id.TextView_Ask_And_Show);
radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
boy_RadioButton = (RadioButton) findViewById(R.id.RadioButton_Boy);
girl_RadioButton = (RadioButton) findViewById(R.id.RadioButton_Gril);
answer_Button = (Button) findViewById(R.id.answer_The_Q_button);
clean_Button = (Button) findViewById(R.id.clean_The_Q_button);
/* 答案数组 */
boy_girl = new String[] { "Boy", "Girl","Boy", "Girl",
"Boy", "Girl","Boy", "Girl",
"Boy" };
/* 按钮设置成不可选 */
answer_Button.setEnabled(false);
clean_Button.setEnabled(false);

/* 给单RadioGroup添加状态改变监听器 */
radioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group,int checkedId){
// TODO Auto-generated method stub
/* 判断显示 */
if (boy_RadioButton.isChecked()) {
answer_TextView.setText(R.string.iam_Boy);
} else {
answer_TextView.setText(R.string.iamGirl);
}
}
});
boy_RadioButton.setOnClickListener(new RadioGroup.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* 按钮设置成可选 */
answer_Button.setEnabled(true);
clean_Button.setEnabled(true);
}
});
girl_RadioButton.setOnClickListener(new RadioGroup.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* 按钮设置成可选 */
answer_Button.setEnabled(true);
clean_Button.setEnabled(true);
}
});
/* 设置按钮事件监听器 */
answer_Button.setOnClickListener(this);
clean_Button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.answer_The_Q_button) {
if (boy_RadioButton.isChecked()) {
checkTheAnswer("Boy");
} else if (girl_RadioButton.isChecked()) {
checkTheAnswer("Girl");
}
}
if (v.getId() == R.id.clean_The_Q_button) {
/* 按钮设置成未选取 */
boy_RadioButton.setChecked(false);
girl_RadioButton.setChecked(false);
/* 按钮设置成不可选 */
answer_Button.setEnabled(false);
clean_Button.setEnabled(false);
answer_TextView.setText(R.string.ask);
}
}
private void checkTheAnswer(String checkstring) {
// TODO Auto-generated method stub
/*检测提示*/
Toast.makeText(this, "检测答案…………", Toast.LENGTH_SHORT).show();
/*获取随机数*/
Random random = new Random();
int index = random.nextInt(9);
if (boy_girl[index].equals(checkstring)) {
/*回答正确 ,设置提示对话框,显示结果*/
showRightorNot.setIcon(R.drawable.right);
showRightorNot.setTitle(R.string.showAnswerTitle_YES);
showRightorNot.setPositiveButton(R.string.about_dialog_ok, null);
showRightorNot.setMessage(R.string.right).show();
Toast.makeText(this,getString(R.string.answerIS)
+boy_girl[index],Toast.LENGTH_LONG).show();
} else {
/*回答错误 ,设置提示对话框,显示结果*/
showRightorNot.setIcon(R.drawable.wrong);
showRightorNot.setTitle(R.string.showAnswerTitle_NO);
showRightorNot.setPositiveButton(R.string.about_dialog_ok, null);

showRightorNot.setMessage(R.string.wrong).show();
Toast.makeText(this,getString(R.string.answerIS)+
boy_girl[index], Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
/*添加退出菜单*/
exit=menu.add("Exit");
/*设置退出菜单图片*/
exit.setIcon(R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
/*结束Activity*/
finish();
return super.onOptionsItemSelected(item);
}
}

 

posted @ 2013-12-24 15:07  爱编程hao123  阅读(174)  评论(0编辑  收藏  举报