Android RadioButton 学习笔记

RadioButton 学习笔记

1.RadioButton要在RadioGroup才有效。代码如下

 <RadioGroup android:id="@+id/radioBtnGroup" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <RadioButton android:layout_width="wrap_content" android:id="@+id/radioMan" android:text="@string/man" android:checked="true"/>
        <RadioButton android:layout_width="wrap_content" android:id="@+id/radioWoman" android:text="@string/woman"/>
  </RadioGroup>

2.Activity中获得RadioGroup,并添加事件监听

RadioGroup radioGroup=(RadioGroup)findViewById(R.id.radioBtnGroup);

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int radionIndex=arg0.getCheckedRadioButtonId();//获得选中的RadioButton 
RadioButton sbutton=(RadioButton) findViewById(radionIndex);
TextView sexText=(TextView)findViewById(R.id.sexText);
sexText.setText(sbutton.getText());
}
});

RadioGroup  添加的是事件监听类型是android.widget.RadioGroup.OnCheckedChangeListener。

你可能感兴趣的:(Android RadioButton 学习笔记)