在andriod中,单选通过由RadioButton组成的RadioGroup实现,复选(多选)通过CheckBox 实现。
单选框的XML文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是傻B吗?"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="肯定是" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当然是" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:id="@+id/submit" />
</LinearLayout>
新建一个活动,在活动中实现对这些选项的逻辑操作
public class AtyUsingRadioGroup extends Activity {
private RadioButton radioright;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aty_using_radiogroup);
radioright = (RadioButton) findViewById(R.id.radioRight);
findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (radioright.isChecked() == true) {
Toast.makeText(getApplicationContext(), String.format("回答正确"), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), String.format("我也觉得"), Toast.LENGTH_SHORT).show();
}
}
});
}
}
这里只绑定了正确选项的RadioButton的ID,它通过isChecked返回的布尔型变量判断是否被选中,同样还是在OnClickListener的onclick方法中写点击“提交”后的操作,通过一个Toast来给用户结果。
一个RadioGroup中只能有一个RadioButton处于被选中状态,这是和复选框最大的区别。
复选框通过CheckBox来实现,它的XML布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="下面哪些游戏是单机游戏?"
android:id="@+id/textView2" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="英雄联盟"
android:id="@+id/LOL"
android:checked="false" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使命召唤"
android:id="@+id/COD"
android:checked="false" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色警戒"
android:id="@+id/RA"
android:checked="false" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CS:GO"
android:id="@+id/CSGO"
android:checked="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:id="@+id/btnSubmit" />
</LinearLayout>
在相应的activity的代码如下:
public class AtyUsingCheckBox extends AppCompatActivity {
private CheckBox cbLOL, cbCOD, cbRA, cbCSGO;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aty_using_checkbox);
submit = (Button) findViewById(R.id.btnSubmit);
cbLOL = (CheckBox) findViewById(R.id.LOL);
cbCOD = (CheckBox) findViewById(R.id.COD);
cbRA = (CheckBox) findViewById(R.id.RA);
cbCSGO = (CheckBox) findViewById(R.id.CSGO);
submit.setOnClickListener(new MyListener());
}
class MyListener implements View.OnClickListener {
@Override
public void onClick(View v) {
if (cbCOD.isChecked() && cbRA.isChecked() && cbLOL.isChecked() == false && cbCSGO.isChecked() == false) {
new AlertDialog.Builder(AtyUsingCheckBox.this)
.setTitle("结果")
.setMessage("正确")
.setPositiveButton("确认", null)
.show();
} else {
new AlertDialog.Builder(AtyUsingCheckBox.this)
.setTitle("结果")
.setMessage("错误")
.setNegativeButton("确认", null)
.show();
}
}
}
}
这里把监听器单独写了一个类,AlertDialog是对话框,用Builder方法可以实现一条语句初始化。setTitle是设置标题,setMessage是设置内容,setNagativeButton,setPositiveButton和setNeuturalButton分别是用户如果做出肯定,否定,中立的回答时的操作,第一个参数是按钮上的文字,第二个参数是点击后的操作,类型是OnClickListener,如果不做操作直接设为null即可。