TextView tv = null;//根据不同选项所要变更的文本控件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根据ID找到该文本控件
tv = (TextView)this.findViewById(R.id.tvSex);
//根据ID找到RadioGroup实例
RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup);
//绑定一个匿名监听器
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
//获取变更后的选中项的ID
int radioButtonId = arg0.getCheckedRadioButtonId();
//根据ID获取RadioButton的实例
RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId);
//更新文本内容,以符合选中项
tv.setText("您的性别是:" + rb.getText());
}
});
}<span style="white-space:pre"> </span>
RadioGroup按键监控的一种写法
最新推荐文章于 2022-03-23 17:06:04 发布