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>
复制粘贴即可测试。