单选按钮在我们实际开发中非常实用,今天就实现一个单选按钮的基本操作,虽然很简单,但是实际动手实现一下对我们这些初学者还是很有必要的,先上效果吧(貌似没有出现动画效果):
首先我们知道,每一组单选按钮都是由一个RadioGroup包裹,这样就能实现单选的效果。
这里radioButton我用了selector配置文件实现,我们很少使用android原生的button控件,为了美观,往往都会重新制作button图片,这里多说几句,在设置selector配置文件的时候,item的状态是分先后顺序的,在配置的时候应该注意:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/cart_selected_active"></item>
<item android:drawable="@drawable/cart_select_normal"/>
</selector>
</span>
然后是配置文件,一个RadioGroup包裹两个RadioButton:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white">
<RadioGroup
android:id="@+id/rg_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_teacher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/teacher"
android:textColor="@color/black"
android:checked="true"
android:button="@drawable/radio_button"/>
<RadioButton
android:id="@+id/rb_student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/radio_margin"
android:text="@string/student"
android:textColor="@color/black"
android:button="@drawable/radio_button"/>
</RadioGroup>
</LinearLayout>
</span>
主要的还是在Activity中实现对RadioButton的setOnCheckedChangeListener监听,在onCheckedChanged方法中判断所选按钮的id是哪一个radioButton的id:
<span style="font-size:18px;">package com.wj.test_sdk;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class OtherActivity extends Activity implements OnCheckedChangeListener {
private Context mContext;
private RadioGroup rg_teacher_student;
private RadioButton rb_teacher ,rb_student;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mContext=this;
setContentView(R.layout.activity_other);
rg_teacher_student=(RadioGroup) findViewById(R.id.rg_select);
rb_teacher=(RadioButton) findViewById(R.id.rb_teacher);
rb_student=(RadioButton) findViewById(R.id.rb_student);
rg_teacher_student.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId==rb_teacher.getId()){
Toast.makeText(mContext, getResources().getString(R.string.teacher), Toast.LENGTH_SHORT).show();
}else if(checkedId==rb_student.getId()){
Toast.makeText(mContext, getResources().getString(R.string.student), Toast.LENGTH_SHORT).show();
}
}
}
</span>
以上操作基本可以满足我们一般时候对单选按钮的操作。当然,我们可以对单个radioButton设置监听
<span style="font-size:18px;">@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Log.e("wj", "isChecked:"+isChecked);
}</span>
这两个onCheckedChanged方法是不同的,因为radioButton的监听是继承自android.widget.CompoundButton.OnCheckedChangeListener,这里我们注意下就可以。以上方法,应该可以满足我们大部分时候对单选按钮的需求,如果还有特殊需要,我们留待以后再考虑。