android单选多选按钮怎么使用-mile米乐体育

android单选多选按钮怎么使用

这篇文章主要介绍了android单选多选按钮怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇android单选多选按钮怎么使用文章都会有所收获,下面我们一起来看看吧。

一、单选按钮

单选按钮类:radiobutton

android:checked="true"设置默认选中

单选按钮控件通常与radiogroup搭配使用。

  • radiogroup是linearlayout的子类,用于将多个单选按钮组合为一组。

  • 同一按钮组内的单选按钮只能有一个被选中。

二、多选按钮

用法基本与button相同

checkbox对象.ischecked()方法可以用来判断复选按钮是否选中

效果图(单选多选写在一个项目里边,用了一个页面跳转):

项目目录:

多选按钮,两种形式

代码:

activity_main.xml

mainactivity.java

packagecom.example.radioandcheckdemo;importandroid.app.activity;importandroid.content.intent;importandroid.os.bundle;importandroid.view.menu;importandroid.view.menuitem;importandroid.view.view;importandroid.view.view.onclicklistener;importandroid.widget.button;publicclassmainactivityextendsactivityimplementsonclicklistener{privatebuttonbutton1;privatebuttonbutton2;@overrideprotectedvoidoncreate(bundlesavedinstancestate){super.oncreate(savedinstancestate);setcontentview(r.layout.activity_main);button1=(button)findviewbyid(r.id.button1);button2=(button)findviewbyid(r.id.button2);button1.setonclicklistener(this);button2.setonclicklistener(this);}@overridepublicvoidonclick(viewv){intentintent=newintent();switch(v.getid()){caser.id.button1://跳转页面intent.setclass(mainactivity.this,radioactivity.class);startactivity(intent);break;caser.id.button2://跳转页面intent.setclass(mainactivity.this,checkactivity.class);startactivity(intent);default:break;}}}

activity_radio.xml

radioactivity.java

packagecom.example.radioandcheckdemo;importandroid.app.activity;importandroid.os.bundle;importandroid.widget.radiobutton;importandroid.widget.radiogroup;importandroid.widget.radiogroup.oncheckedchangelistener;importandroid.widget.toast;publicclassradioactivityextendsactivityimplementsoncheckedchangelistener{privateradiogroupgroup1;privateradiogroupgroup2;@overrideprotectedvoidoncreate(bundlesavedinstancestate){super.oncreate(savedinstancestate);setcontentview(r.layout.activity_radio);group1=(radiogroup)findviewbyid(r.id.group1);group2=(radiogroup)findviewbyid(r.id.group2);group1.setoncheckedchangelistener(this);group2.setoncheckedchangelistener(this);}@overridepublicvoidoncheckedchanged(radiogroupgroup,intcheckedid){//显示值的几种方法//checkedid选中radiobutton的id/*switch(checkedid){caser.id.radio1:toast.maketext(this,"男",toast.length_long).show();break;caser.id.radio2:toast.maketext(this,"女",toast.length_long).show();break;caser.id.radio3:toast.maketext(this,"吃了",toast.length_long).show();break;caser.id.radio4:toast.maketext(this,"没吃",toast.length_long).show();break;default:break;}*///找到点击的radiobutton//radiobuttonradio=(radiobutton)findviewbyid(checkedid);//取出radiobutton中的值//stringstr=radio.gettext().tostring();//弹框显示选中的值//toast.maketext(this,str,toast.length_long).show();//两组数据同时显示//根据radiogroup取出数据,没有选中返回-1stringstr="";intbuttonid=group1.getcheckedradiobuttonid();if(buttonid!=-1){radiobuttonradio=(radiobutton)findviewbyid(buttonid);str="你的性别是" radio.gettext().tostring();}else{str="你没有选择性别";}buttonid=group2.getcheckedradiobuttonid();if(buttonid!=-1){radiobuttonradio=(radiobutton)findviewbyid(buttonid);str =",你吃饭了吗?" radio.gettext().tostring();}toast.maketext(this,str,toast.length_long).show();}}

activity_check.xml

checkactivity.java

packagecom.example.radioandcheckdemo;importandroid.app.activity;importandroid.os.bundle;importandroid.view.menu;importandroid.view.menuitem;importandroid.view.view;importandroid.view.view.onclicklistener;importandroid.widget.button;importandroid.widget.checkbox;importandroid.widget.compoundbutton;importandroid.widget.compoundbutton.oncheckedchangelistener;importandroid.widget.toast;publicclasscheckactivityextendsactivity{privatecheckboxcheck1;privatecheckboxcheck2;privatecheckboxcheck3;privatecheckboxcheck4;privatebuttonbutton1;privateoncheckedchangelistenerlistenter=newoncheckedchangelistener(){@overridepublicvoidoncheckedchanged(compoundbuttonbuttonview,booleanischecked){//选中多选框checkboxcheck=(checkbox)buttonview;//取出当前勾选值stringstr=check.gettext().tostring();//判断是否勾选状态if(ischecked){str="你学了" str;}else{str="你没学" str;}toast.maketext(checkactivity.this,str,toast.length_long).show();}};@overrideprotectedvoidoncreate(bundlesavedinstancestate){super.oncreate(savedinstancestate);setcontentview(r.layout.activity_check);check1=(checkbox)findviewbyid(r.id.check1);check2=(checkbox)findviewbyid(r.id.check2);check3=(checkbox)findviewbyid(r.id.check3);check4=(checkbox)findviewbyid(r.id.check4);button1=(button)findviewbyid(r.id.button1);//多选框点击事件/*check1.setoncheckedchangelistener(listenter);check2.setoncheckedchangelistener(listenter);check3.setoncheckedchangelistener(listenter);check4.setoncheckedchangelistener(listenter);*///提交按钮点击事件button1.setonclicklistener(newonclicklistener(){@overridepublicvoidonclick(viewv){stringstr="我学过了";booleanf=false;if(check1.ischecked()){str =check1.gettext() ",";f=true;}if(check2.ischecked()){str =check2.gettext() ",";f=true;}if(check3.ischecked()){str =check3.gettext() ",";f=true;}if(check4.ischecked()){str =check4.gettext() ",";f=true;}if(f){str=str.substring(0,str.length()-1);}toast.maketext(checkactivity.this,str,toast.length_long).show();}});}}

关于“android单选多选按钮怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“android单选多选按钮怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道。

展开全文
内容来源于互联网和用户投稿,文章中一旦含有米乐app官网登录的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系米乐app官网登录删除

最新文章

网站地图