Android-RadioButton(单选按钮)详解

Android-RadioButton(单选按钮)详解

2023年7月7日发(作者:)

Android-RadioButton(单选按钮)详解图⽚来源于⽹络RadioButtonRadioButton是单选按钮,允许⽤户在⼀个组中选择⼀个选项。同⼀组中的单选按钮有互斥效果。RadioButton的特点utton是圆形单选框;roup是个可以容纳多个RadioButton的容器;3.在RadioGroup中的RadioButton控件可以有多个,但同时有且仅有⼀个可以被选中。简单的代码⽰例在布局⽂件中定义RadioGroup,在RadioGroup中添加RadioButton(⾄少两个),在Java代码中获取控件对象为对象添加监听器,实现OnCheckedChangeListener接⼝,(选择RadioGroup包下的那个);重写onCheckedChanged⽅法。XML⽂件``` ```Java⽂件```import patActivity;import ;import ;import utton;import roup;import kedChangeListener;public class MainActivity extends AppCompatActivity{ private RadioGroup rg; private RadioButton rb_Male, rb_Female; @Override protected void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_radiobutton); rg = (RadioGroup) findViewById(_sex); rb_Male = (RadioButton) findViewById(_Male); rb_Female = (RadioButton) findViewById(_FeMale); //注意是给RadioGroup绑定监视器 heckedChangeListener(new MyRadioButtonListener() ); } class MyRadioButtonListener implements OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 选中状态改变时被触发 switch (checkedId) { case _FeMale: // 当⽤户选择⼥性时 Log.i("sex", "当前⽤户选择"+rb_t().toString()); break; case _Male: // 当⽤户选择男性时 Log.i("sex", "当前⽤户选择"+rb_t().toString()); break; } } }}```总结基本的使⽤就是上⾯Demo说的那样。在Android开发当中,我们也可以使⽤RadioButton控件来实现应⽤的底部导航栏。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1688676256a161723.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信