今天要给产品加一个类似于调查问卷的页面,用到单选框,百度了一些觉得很零碎也不详细,觉得自己写一篇学习笔记方便自己也方便别人学习。
RadioButton
RadioButton是单选框,比如男女,年龄阶段的单项选择等。一般会有一组Button,放在一个RadioGroup里面。这样他自己就会是单选框。但是很多人也觉得原生的按钮太丑了,所以我就自定义了一下。
RadioButton和RadioGroup的关系:
1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
2、每个RadioGroup中的RadioButton同时只能有一个被选中
3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中
4、大部分场合下,一个RadioGroup中至少有2个RadioButton
5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置
而我这里算是把原点改成方块对勾的选择方式,所以就有设置android:drawableLeft=“@drawable/radio_loginquest_bt”,其中的@drawable/radio_loginquest_bt 即为我们一开始在drawable下编写的图片样式。默认该选项选上的属性是android:checked="true"。
有的朋友想设置选框的大小尺寸,可以试一下下面的方法设置一下边界:
drawables = getCompoundDrawables();
drawables[1].setBounds();
setCompoundDrawables(drawables[0],drawables[1],drawables[2].drawables[3]);
选框太多,我没用亲自试,大家可以试一下告诉我行不行或者具体写法,我直接把图片的尺寸改小了一下就好了。
Java中单选框的监听事件如下:
RadioButton
RadioButton是单选框,比如男女,年龄阶段的单项选择等。一般会有一组Button,放在一个RadioGroup里面。这样他自己就会是单选框。但是很多人也觉得原生的按钮太丑了,所以我就自定义了一下。
RadioButton和RadioGroup的关系:
1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
2、每个RadioGroup中的RadioButton同时只能有一个被选中
3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中
4、大部分场合下,一个RadioGroup中至少有2个RadioButton
5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置
先上一张做好的效果图吧,再看看怎么实现的:
选好两张图片,分别为点击状态和未点击状态,在res/drawable中定义编写如下样式:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/login_squestion" android:state_checked="true"/>
<item android:drawable="@drawable/login_uquestion" android:state_checked="false"/>
</selector>
在XML布局中,添加组和控件:
<RadioGroup
android:id="@+id/question_rdGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="40dp"
android:orientation="horizontal" >
<RadioButton
style="@style/radioBtn"
android:text="@string/loginques_best" />
<RadioButton
style="@style/radioBtn"
android:text="@string/loginques_better" />
<RadioButton
style="@style/radioBtn"
android:text="@string/loginques_commonly" />
<RadioButton
style="@style/radioBtn"
android:text="@string/loginques_worse" />
<RadioButton
style="@style/radioBtn"
android:text="@string/loginques_worst" />
</RadioGroup>
由于我的控件太多,相同的属性就全部合并到style里面了,不然东西太多,修改起来太麻烦:
<style name="radioBtn">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:drawableLeft">@drawable/radio_loginquest_bt</item>
<item name="android:drawablePadding">5dp</item>
<item name="android:textSize">14sp</item>
<item name="android:layout_marginLeft">5dp</item>
</style>
如果想实现整个RadioButton控件,点击填充变色的效果,则许修改android:background="@drawable/radio" 这一样式 。android:button="@null" 去除RadioButton前面的圆点。而我这里算是把原点改成方块对勾的选择方式,所以就有设置android:drawableLeft=“@drawable/radio_loginquest_bt”,其中的@drawable/radio_loginquest_bt 即为我们一开始在drawable下编写的图片样式。默认该选项选上的属性是android:checked="true"。
有的朋友想设置选框的大小尺寸,可以试一下下面的方法设置一下边界:
drawables = getCompoundDrawables();
drawables[1].setBounds();
setCompoundDrawables(drawables[0],drawables[1],drawables[2].drawables[3]);
选框太多,我没用亲自试,大家可以试一下告诉我行不行或者具体写法,我直接把图片的尺寸改小了一下就好了。
Java中单选框的监听事件如下:
rdGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
String question1 = radioButton.getText().toString();
}
});
question1中可以获得这组RadioButton中选中Button的文字,根据文字进行一些逻辑判断即可。也有朋友是下面的这种写法,大体差不多,看个人的想法和习惯就好。
//对控件对象进行声明
private RadioGroup genderGroup=null;
private RadioButton femaleRadioButton=null;
private RadioButton maleRadioButton=null;
private EditText editText1=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过控件的ID来得到代表控件的对象
genderGroup=(RadioGroup)findViewById(R.id.genderGroup);
femaleRadioButton=(RadioButton)findViewById(R.id.femaleButton);
maleRadioButton=(RadioButton)findViewById(R.id.maleButton);
editText1=(EditText)findViewById(R.id.editText1);
//给RadioGroup设置事件监听
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId==femaleRadioButton.getId()){
editText1.setText("女性");
}else if(checkedId==maleRadioButton.getId()){
editText1.setText("男性");
}
}
});
}
Checkbox 是Android控件中拥有着0和1这种特性的魔力控件。一般用来实现多选。
先来讲讲Checkbox的基本使用.在XML中定义。
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cbx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false" />
在Activity中使用:
CheckBox cbx = (CheckBox) findViewById(R.id.cbx);
cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do something
}
});
要注意的是,CheckBox本身是一个视图,是展示给用户看的,因此我们要用数据来控制它的展示.所以,我们的CheckBox在Activity中要这么写:
boolean isChecked= false;
CheckBox cbx = (CheckBox) findViewById(R.id.cbx);
cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//do something
}else{
//do something else
}
}
});
cbx.setChecked(isChecked);
这样,我们改变数据的时候,视图的状态就会跟着数据来做改变了.注意,监听器一定要这setChecked之前设置,这样才能体现出来数据来控制视图的展示。