本文实例为大家分享了android单选多选按钮使用的具体代码,供大家参考,具体内容如下
一、单选按钮
单选按钮类:radiobutton
android:checked=”true”设置默认选中
单选按钮控件通常与radiogroup搭配使用。
- radiogroup是linearlayout的子类,用于将多个单选按钮组合为一组。
- 同一按钮组内的单选按钮只能有一个被选中。
二、多选按钮
用法基本与button相同
checkbox对象.ischecked()方法可以用来判断复选按钮是否选中
效果图(单选多选写在一个项目里边,用了一个页面跳转):
项目目录:
多选按钮,两种形式
代码:
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativepackage}.${activityclass}" > <button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="radioactivity单选" /> <button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="checkactivity多选" /> </linearlayout>
mainactivity.java
package com.example.radioandcheckdemo; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class mainactivity extends activity implements onclicklistener{ private button button1; private button button2; @override protected void oncreate(bundle savedinstancestate) { 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); } @override public void onclick(view v) { intent intent = new intent(); switch (v.getid()) { case r.id.button1: //跳转页面 intent.setclass(mainactivity.this, radioactivity.class); startactivity(intent); break; case r.id.button2: //跳转页面 intent.setclass(mainactivity.this, checkactivity.class); startactivity(intent); default: break; } } }
activity_radio.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="20sp" tools:context="${relativepackage}.${activityclass}" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <!-- 单选 android:checked="true"设置默认选中 --> <radiogroup android:id="@+id/group1" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <radiobutton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男"/> <radiobutton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女"/> </radiogroup> <!-- 分界线 --> <view android:layout_width="match_parent" android:layout_height="2sp" android:background="@android:color/holo_blue_dark" android:layout_margintop="10sp" android:layout_marginbottom="10sp" /> <textview android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="18sp" android:text="你吃饭了吗?"/> <radiogroup android:id="@+id/group2" android:layout_width="match_parent" android:layout_height="wrap_content" > <radiobutton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="吃了"/> <radiobutton android:id="@+id/radio4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="没吃"/> </radiogroup> </linearlayout>
radioactivity.java
package com.example.radioandcheckdemo; import android.app.activity; import android.os.bundle; import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.radiogroup.oncheckedchangelistener; import android.widget.toast; public class radioactivity extends activity implements oncheckedchangelistener { private radiogroup group1; private radiogroup group2; @override protected void oncreate(bundle savedinstancestate) { 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); } @override public void oncheckedchanged(radiogroup group, int checkedid) { //显示值的几种方法 //checkedid选中radiobutton的id /*switch (checkedid) { case r.id.radio1: toast.maketext(this, "男", toast.length_long).show(); break; case r.id.radio2: toast.maketext(this, "女", toast.length_long).show(); break; case r.id.radio3: toast.maketext(this, "吃了", toast.length_long).show(); break; case r.id.radio4: toast.maketext(this, "没吃", toast.length_long).show(); break; default: break; }*/ //找到点击的radiobutton //radiobutton radio = (radiobutton) findviewbyid(checkedid); //取出radiobutton中的值 //string str = radio.gettext().tostring(); //弹框显示选中的值 //toast.maketext(this, str, toast.length_long).show(); //两组数据同时显示 //根据radiogroup取出数据,没有选中返回-1 string str = ""; int buttonid = group1.getcheckedradiobuttonid(); if(buttonid != -1){ radiobutton radio = (radiobutton) findviewbyid(buttonid); str = "你的性别是" + radio.gettext().tostring(); }else{ str = "你没有选择性别"; } buttonid = group2.getcheckedradiobuttonid(); if(buttonid != -1){ radiobutton radio = (radiobutton) findviewbyid(buttonid); str += ", 你吃饭了吗?"+radio.gettext().tostring(); } toast.maketext(this, str, toast.length_long).show(); } }
activity_check.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativepackage}.${activityclass}" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择所学课程:" /> <checkbox android:id="@+id/check1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="html" /> <checkbox android:id="@+id/check2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="c" /> <checkbox android:id="@+id/check3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="php" /> <checkbox android:id="@+id/check4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="java" /> <button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" /> </linearlayout>
checkactivity.java
package com.example.radioandcheckdemo; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.checkbox; import android.widget.compoundbutton; import android.widget.compoundbutton.oncheckedchangelistener; import android.widget.toast; public class checkactivity extends activity { private checkbox check1; private checkbox check2; private checkbox check3; private checkbox check4; private button button1; private oncheckedchangelistener listenter = new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { //选中多选框 checkbox check = (checkbox)buttonview; //取出当前勾选值 string str = check.gettext().tostring(); //判断是否勾选状态 if(ischecked){ str = "你学了"+str; }else{ str = "你没学"+str; } toast.maketext(checkactivity.this, str, toast.length_long).show(); } }; @override protected void oncreate(bundle savedinstancestate) { 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(new onclicklistener() { @override public void onclick(view v) { string str = "我学过了"; boolean f = 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(); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。