|
RadioButton 单选
RadioGroup 组与onCheckedChanged 事件
①新建工程
②string.xml 中添加字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Ex_Ctrl_6</string>
<string name="iam_Boy">帅哥</string>
<string name="iamGirl">美女</string>
<string name="ask">请问你是??</string>
</resources>
复制代码
③修改 mian.xml 布局,添加 UI 元素
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ask"
android:id="@+id/TextView_Ask_And_Show"
android:textSize="25px"/>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/RadioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioButton_Boy"
android:text="@string/iam_Boy"></RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/iamGirl"
android:id="@+id/RadioButton_Gril"></RadioButton>
</RadioGroup>
</LinearLayout>
复制代码
④修改 mainActivity.java 文件
package zyf.Ex_Ctrl_6;
import
import
import
import
import
android.app.Activity;
android.os.Bundle;
android.widget.RadioButton;
android.widget.RadioGroup;
android.widget.TextView;
public class Ex_Ctrl_6 extends Activity {
/** Called when the activity is first created. */
private TextView answer_TextView;
private RadioButton boy_RadioButton,girl_RadioButton;
private RadioGroup radioGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 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);
/*给单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);
}
}
});
}
}
复制代码
⑤结果
RadioButton 猜猜看
猜猜我是??
①新建项目
②准备三张 png 图片
<resources>
复制代码
④在 main.xml 中添加 UI 元素
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ask"
android:id="@+id/TextView_Ask_And_Show"
android:textSize="25px"/>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/RadioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioButton_Boy"
android:text="@string/iam_Boy">
</RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/iamGirl"
android:id="@+id/RadioButton_Gril">
</RadioButton>
</RadioGroup>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/answer_The_Q_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/answer_Q"
></Button>
<Button
android:id="@+id/clean_The_Q_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clean"
></Button>
</LinearLayout>
</LinearLayout>
复制代码
⑤修改 mainActivity.java 文件
package zyf.Ex_Ctrl_6;
import
import
import
import
import
java.util.Random;
android.app.Activity;
android.app.AlertDialog;
android.os.Bundle;
android.view.Menu;
import
import
import
import
import
import
import
android.view.MenuItem;
android.view.View;
android.widget.Button;
android.widget.RadioButton;
android.widget.RadioGroup;
android.widget.TextView;
android.widget.Toast;
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);
}
}
复制代码结果:
|
|