自定义RadioButton

Android提供的RadioButton样式和功能十分有限,在使用多个RadioButton进行单选的时候,RadioGroup并不好用,比如说它只能横着或者竖着排,却不能用其他排列方式。在这里自己没事瞎写一个自定义的RadioButton,实现效果如下:

 

源码如下:RadioButtonActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class RadioButtonActivity extends Activity implements OnCheckedChangeListener{
	RadioButton rb1;
	RadioButton rb2;
	RadioButton rb3;
	RadioButton rb4;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        rb1 = (RadioButton)findViewById(R.id.radioButton1);
        rb2 = (RadioButton)findViewById(R.id.radioButton2);
        rb3 = (RadioButton)findViewById(R.id.radioButton3);
        rb4 = (RadioButton)findViewById(R.id.radioButton4);
        
        rb1.setOnCheckedChangeListener(this);
        rb2.setOnCheckedChangeListener(this);
        rb3.setOnCheckedChangeListener(this);
        rb4.setOnCheckedChangeListener(this);
    }
   
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		switch(buttonView.getId())
		{
		case R.id.radioButton1:
			if(isChecked == true){
				rb1.setChecked(isChecked);
				rb2.setChecked(!isChecked);
				rb3.setChecked(!isChecked);
				rb4.setChecked(!isChecked);
				isChecked = false;
			}

			break;
		case R.id.radioButton2:
			if(isChecked == true){
				rb1.setChecked(!isChecked);
				rb2.setChecked(isChecked);
				rb3.setChecked(!isChecked);
				rb4.setChecked(!isChecked);
				isChecked = false;
			}

			break;
		case R.id.radioButton3:
			if(isChecked == true){
				rb1.setChecked(!isChecked);
				rb2.setChecked(!isChecked);
				rb3.setChecked(isChecked);
				rb4.setChecked(!isChecked);
				isChecked = false;
			}

			break;
		case R.id.radioButton4:
			if(isChecked == true){
				rb1.setChecked(!isChecked);
				rb2.setChecked(!isChecked);
				rb3.setChecked(!isChecked);
				rb4.setChecked(isChecked);
				isChecked = false;
			}

			break;
		default:
			break;
		}
	}
}

Blog地址http://blog.csdn.net/kira012345/article/details/6585125

布局文件:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    >
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:orientation="horizontal">
        <RadioButton android:text="RadioButton1" android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton>
        <RadioButton android:text="RadioButton2" android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton>
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:orientation="horizontal">
        <RadioButton android:text="RadioButton3" android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton>
        <RadioButton android:text="RadioButton4" android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton>
    </LinearLayout>
</LinearLayout>


复制粘贴即可测试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值