Android之RaidoButton和Checkbox的使用
在布局文件中,RaidoButton需要放在RadioGroup中。代表一组中只能选中一个。
layout文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/radio_tv" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/mailRadio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/mailRadio" /> <RadioButton android:id="@+id/femailRadio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/femailRadio" /> </RadioGroup> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/checkbox_tv" /> <CheckBox android:id="@+id/read_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read_checkbox" /> <CheckBox android:id="@+id/watchtv_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/watchtv_checkbox" /> <CheckBox android:id="@+id/playgame_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/playgame_checkbox" /> </LinearLayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="mailRadio">男</string> <string name="femailRadio">女</string> <string name="radio_tv">请选择性别:</string> <string name="checkbox_tv">请选择爱好:</string> <string name="watchtv_checkbox">看电视</string> <string name="read_checkbox">看书</string> <string name="playgame_checkbox">打游戏</string> <string name="radio_checkbox_text">单选和多选按钮的使用</string> </resources>
java:
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; public class RadioAndCheckboxActivity extends Activity { private RadioGroup radioGroup = null; private RadioButton maleRadio = null; private RadioButton femailRadio = null; private CheckBox readCheckbox = null; private CheckBox watchTvCheckBox = null; private CheckBox playgameCheckBox = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.radio_checkbox); radioGroup = (RadioGroup) findViewById(R.id.radioGroup); maleRadio = (RadioButton) findViewById(R.id.mailRadio); femailRadio = (RadioButton) findViewById(R.id.femailRadio); readCheckbox = (CheckBox) findViewById(R.id.read_checkbox); watchTvCheckBox = (CheckBox) findViewById(R.id.watchtv_checkbox); playgameCheckBox = (CheckBox) findViewById(R.id.playgame_checkbox); // 在RadioGroup上添加监听器 radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { System.out.println(checkedId); if (maleRadio.getId() == checkedId) { Toast.makeText(RadioAndCheckboxActivity.this, maleRadio.getText(), Toast.LENGTH_SHORT).show(); } else if (femailRadio.getId() == checkedId) { Toast.makeText(RadioAndCheckboxActivity.this, femailRadio.getText(), Toast.LENGTH_SHORT).show(); } } }); readCheckbox.setOnCheckedChangeListener(new MyCheckboxCheckedChangeListener()); watchTvCheckBox.setOnCheckedChangeListener(new MyCheckboxCheckedChangeListener()); playgameCheckBox.setOnCheckedChangeListener(new MyCheckboxCheckedChangeListener()); System.out.println(readCheckbox.getText()); } class MyCheckboxCheckedChangeListener implements CompoundButton.OnCheckedChangeListener { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { String msg = buttonView.getText().toString(); Log.i("RadioAndCheckboxActivity", msg + "-->checked:" + isChecked); if (isChecked) { Toast.makeText(RadioAndCheckboxActivity.this, msg + " Checked!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(RadioAndCheckboxActivity.this, msg + " Not checked!", Toast.LENGTH_SHORT).show(); } } } }
注意2点:
1.在RadioGroup上添加OnCheckedChangeListener,不是在RadioButton。
2.CheckBox添加的Listener是CompoundButton.OnCheckedChangeListener,Checkbox继承CompoundButton,使用的CompoundButton的OnCheckedChangeListener。
效果:
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28