对运营网站有什么见解,海口网页设计公司排名,青州网站设计公司,公司怎么建网站做推广一、单选框#xff08;RadioGroup#xff09;
单选框#xff08;RadioGroup#xff09;需要配合单选按钮#xff08;RadioButton#xff09;使用#xff0c;同一个单选框中的单选按钮只能被选中一个#xff0c;默认是一个都不选中。
RadioGroup的常见属性#xff08…一、单选框RadioGroup
单选框RadioGroup需要配合单选按钮RadioButton使用同一个单选框中的单选按钮只能被选中一个默认是一个都不选中。
RadioGroup的常见属性在XML中配置和方法在Java代码中使用包括
如果需要获取被选中的按钮的文本内容可以在setOnCheckedChangeLinstener()方法中如下获取
Override
public void onCheckedChanged(RadioGroup group, int checkedId) {RadioButton button findViewById(group.getCheckedRadioButtonId());Toast.makeText(MainActivity.this, button.getText() 被选中, Toast.LENGTH_SHORT).show();
}
如果要在没有改变的情况下获取当前被选中按钮的文本内容也可以使用类似的代码但是需要判断button是否为null否则程序会退出。
radioGroup.clearCheck();
RadioButton button findViewById(radioGroup.getCheckedRadioButtonId());
if(button ! null){Toast.makeText(MainActivity.this, button.getText() 被选中,Toast.LENGTH_SHORT).show();
}else{Toast.makeText(MainActivity.this, 没有按钮被选中, Toast.LENGTH_SHORT).show();
}
当然onCheckedChanged()方法中更多的是使用switch方法
Override
public void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.radio_man:Toast.makeText(MainActivity.this, 您选择了男性, Toast.LENGTH_SHORT).show();break;case R.id.radio_women:Toast.makeText(MainActivity.this, 您选择了女性, Toast.LENGTH_SHORT).show();break;}
}
待续。。。