radiobutton控件(讲解python常用框架)

2022-07-18,,,,

给大家带来的是andoird基本ui控件中的radiobutton和checkbox; 先说下本节要讲解的内容是:radiobutton和checkbox的

1.基本用法

2.事件处理;

3.自定义点击效果;

4.改变文字与选择框的相对位置;

5.修改文字与选择框的距离

其实这两个控件有很多地方都是类似的,除了单选和多选,事件处理,其他的都是类似的! 另外还有一个listview上checkbox的错位的问题,我们会在listview那一章对这个问题进行 解决,好的,开始本节内容~ 本节官方文档api:radiobutton;checkbox;


1.基本用法与事件处理:


1)radiobutton(单选按钮)

如题单选按钮,就是只能够选中一个,所以我们需要把radiobutton放到radiogroup按钮组中,从而实现 单选功能!先熟悉下如何使用radiobutton,一个简单的性别选择的例子: 另外我们可以为外层radiogroup设置orientation属性然后设置radiobutton的排列方式,是竖直还是水平~

效果图:

ps:笔者的手机是android 5.0.1的,这里的radiobutton相比起旧版本的radiobutton,稍微好看一点~

布局代码如下:

<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=”.mainactivity” >

<textview

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”请选择性别”

android:textsize=”23dp”

/>

<radiogroup

android:id=”@+id/radiogroup”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:orientation=”horizontal”>

<radiobutton

android:id=”@+id/btnman”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”男”

android:checked=”true”/>

<radiobutton

android:id=”@+id/btnwoman”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”女”/>

</radiogroup>

<button

android:id=”@+id/btnpost”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”提交”/>

</linearlayout>

获得选中的值:

这里有两种方法,

第一种是为radiobutton设置一个事件监听器setoncheckchangelistener

例子代码如下:

radiogroup radgroup = (radiogroup) findviewbyid(r.id.radiogroup);

//第一种获得单选按钮值的方法

//为radiogroup设置一个监听器:setoncheckedchanged()

radgroup.setoncheckedchangelistener(new oncheckedchangelistener() {

@override

public void oncheckedchanged(radiogroup group, int checkedid) {

radiobutton radbtn = (radiobutton) findviewbyid(checkedid);

toast.maketext(getapplicationcontext(), “按钮组值发生改变,你选了” + radbtn.gettext(), toast.length_long).show();

}

});

运行效果图:

ps:另外有一点要切记,要为每个radiobutton添加一个id,不然单选功能会生效!!!

第二种方法是通过单击其他按钮获取选中单选按钮的值,当然我们也可以直接获取,这个看需求~

例子代码如下:

button btnchange = (button) findviewbyid(r.id.btnpost);

radiogroup radgroup = (radiogroup) findviewbyid(r.id.radiogroup);

//为radiogroup设置一个监听器:setoncheckedchanged()

btnchange.setonclicklistener(new onclicklistener() {

@override

public void onclick(view v) {

for (int i = 0; i < radgroup.getchildcount(); i++) {

radiobutton rd = (radiobutton) radgroup.getchildat(i);

if (rd.ischecked()) {

toast.maketext(getapplicationcontext(), “点击提交按钮,获取你选择的是:” + rd.gettext(), toast.length_long).show();

break;

}

}

}

});

运行效果图:

代码解析: 这里我们为提交按钮设置了一个setonclicklistener事件监听器,每次点击的话遍历一次radiogroup判断哪个按钮被选中我们可以通过下述方法获得radiobutton的相关信息!

  • getchildcount( )获得按钮组中的单选按钮的数目;
  • getchinldat(i):根据索引值获取我们的单选按钮
  • ischecked( ):判断按钮是否选中

2)checkbox(复选框)

如题复选框,即可以同时选中多个选项,至于获得选中的值,同样有两种方式: 1.为每个checkbox添加事件:
setoncheckedchangelistener 2.弄一个按钮,在点击后,对每个checkbox进行判断:ischecked();

运行效果图:

实现代码:

public class mainactivity extends appcompatactivity implements view.onclicklistener,compoundbutton.oncheckedchangelistener{

private checkbox cb_one;

private checkbox cb_two;

private checkbox cb_three;

private button btn_send;

@override

protected void oncreate(bundle savedinstancestate) {

super.oncreate(savedinstancestate);

setcontentview(r.layout.activity_main);

cb_one = (checkbox) findviewbyid(r.id.cb_one);

cb_two = (checkbox) findviewbyid(r.id.cb_two);

cb_three = (checkbox) findviewbyid(r.id.cb_three);

btn_send = (button) findviewbyid(r.id.btn_send);

cb_one.setoncheckedchangelistener(this);

cb_two.setoncheckedchangelistener(this);

cb_three.setoncheckedchangelistener(this);

btn_send.setonclicklistener(this);

}

@override

public void oncheckedchanged(compoundbutton compoundbutton, boolean b) {

if(compoundbutton.ischecked()) toast.maketext(this,compoundbutton.gettext().tostring(),toast.length_short).show();

}

@override

public void onclick(view view) {

string choose = “”;

if(cb_one.ischecked())choose += cb_one.gettext().tostring() + “”;

if(cb_two.ischecked())choose += cb_two.gettext().tostring() + “”;

if(cb_three.ischecked())choose += cb_three.gettext().tostring() + “”;

toast.maketext(this,choose,toast.length_short).show();

}

}


2.自定义点击效果

虽然5.0后的radiobutton和checkbox都比旧版本稍微好看了点,但是对于我们来说 可能还是不喜欢或者需求,需要自己点击效果!实现起来很简单,先编写一个自定义 的selctor资源,设置选中与没选中时的切换图片~!

实现效果图如下:

ps:这里素材的原因,有点小…

<?xml version=”1.0″ encoding=”utf-8″?>

<selector xmlns:android=”http://schemas.android.com/apk/res/android”>

<item

android:state_enabled=”true”

android:state_checked=”true”

android:drawable=”@mipmap/ic_checkbox_checked”/>

<item

android:state_enabled=”true”

android:state_checked=”false”

android:drawable=”@mipmap/ic_checkbox_normal” />

</selector>

写好后,我们有两种方法设置,也可以说一种吧!你看看就知道了~

①android:button属性设置为上述的selctor

android:button=”@drawable/rad_btn_selctor”

②在style中定义一个属性,然后通过android style属性设置,先往style添加下述代码:

<style name=”mycheckbox” parent=”@android:style/widget.compoundbutton.checkbox”>

<item name=”android:button”>@drawable/rad_btn_selctor</item>

</style>

然后布局那里:

style=”@style/mycheckbox”


3.改变文字与选择框的相对位置

这个实现起来也很简单,还记得我们之前学textview的时候用到的drawablexxx吗? 要控制选择框的位置,两部即可!设置:

step 1. android:button=”@null”

step 2. android:drawabletop=”@android:drawable/btn_radio”

当然我们可以把drawablexxx替换成自己喜欢的效果!


4.修改文字与选择框的距离

有时,我们可能需要调节文字与选择框之间的距离,让他们看起来稍微没那么挤,我们可以:

1.在xml代码中控制: 使用android:paddingxxx = “xxx” 来控制距离

2.在java代码中,稍微好一点,动态计算paddingleft!

示例代码如下:

rb.setbuttondrawable(r.drawable.rad_btn_selctor);

int rb_paddingleft = getresources().getdrawable(r.mipmap.ic_checkbox_checked).getintrinsicwidth()+5;

rb.setpadding(rb_paddingleft, 0, 0, 0);


本节小结:

好的,关于radiobutton和checkbox就讲到这里,如果有什么写得不对的,不好的,或者有好的建议欢迎指出 万分感激

《radiobutton控件(讲解python常用框架).doc》

下载本文的Word格式文档,以方便收藏与打印。