android 标签多选单选,Android RadioGroup单选框变成多选问题

在用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" />

布局大網

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

然后是运行后布局的效果:注册时默认选中的,设置是默认不选中,理应当我选中设置时,注册框为不选中,但是出现了图二问题

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

图二:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

本身布局文件写好后默认是为单选功能的,但是却不是。

原因出现在哪里呢,经过排查问题出现在了两个线性布局上,请看图

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

这两个线性布局导致了单选功能失效,具体没有深究,应为这样做也是有需求的,以下是解决这个问题的代码

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);

}

}

};

}

附:其实正常的单选布局应该是这样子的

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png但是这只能满足一般的单选框需求

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值