单选按钮RadioButton 单选分组RadioGroup 每个RadioGroup,就是一个分组,一个分组里,只有一个单选按钮RadioButton 1.XML文科 <RadioGroup/> <RadioButton/> <RadioButton/>是<RadioGroup/>的子标签 <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/nv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> <RadioButton android:text="男" android:id="@+id/nan" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> 2.代码 FindViewById找到控件 3.监听器 OnClickListener,OnCheckedChangeListener private RadioGroup radioGroup; private RadioButton buttonNv; private RadioButton buttonNan; // 别import错,要导radioButton这个包,实现监听接口 class RadioGroupListener implements OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (buttonNan.getId() == checkedId) { System.out.println("女"); } else if (buttonNv.getId() == checkedId) { System.out.println("男"); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup = (RadioGroup) findViewById(R.id.radioGroup); buttonNan = (RadioButton) findViewById(R.id.nan);//2.FindViewById找到控件 buttonNv = (RadioButton) findViewById(R.id.nv); RadioGroupListener listener = new RadioGroupListener(); radioGroup.setOnCheckedChangeListener(listener);3.监听器
安卓入门.单选按钮RadioButton和·RadioGroup
猜你喜欢
转载自8850702.iteye.com/blog/2276016
今日推荐
周排行