在用RadioButton单选框组件时,变成了多选的问题,先看我的布局代码
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/action_registr" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical" >
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/action_settings" />
布局大網
然后是运行后布局的效果:注册时默认选中的,设置是默认不选中,理应当我选中设置时,注册框为不选中,但是出现了图二问题
图二:
本身布局文件写好后默认是为单选功能的,但是却不是。
原因出现在哪里呢,经过排查问题出现在了两个线性布局上,请看图
这两个线性布局导致了单选功能失效,具体没有深究,应为这样做也是有需求的,以下是解决这个问题的代码
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.RadioButton;
public class WebViewActivity extends FragmentActivity {
RadioButton mLeftRadio,mRightRadio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
mLeftRadio = (RadioButton)findViewById(R.id.radioButton1);
mRightRadio = (RadioButton)findViewById(R.id.radioButton2);
mLeftRadio.setOnCheckedChangeListener(mChangeListener);
mRightRadio.setOnCheckedChangeListener(mChangeListener);
}
final OnCheckedChangeListener mChangeListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView.getId()==R.id.radioButton1 && isChecked){
T.showShort(getApplication(), "RadioButton1");
mRightRadio.setChecked(false);
}else if(buttonView.getId()==R.id.radioButton2 && isChecked){
T.showShort(getApplication(), "RadioButton2");
mLeftRadio.setChecked(false);
}
}
};
}
附:其实正常的单选布局应该是这样子的
但是这只能满足一般的单选框需求