多个RadioGroup,只允许一个选中的解决方案

主要关注onCheckedChangeListener方法~


public class ConferenceActivity extends BaseActivity implements OnCheckedChangeListener{


public static final String TAG = "ConferenceActivity";

//function moudle
private RadioGroup mRgConfFunctionModule1;
private RadioGroup mRgConfFunctionModule2;

//used to deal with radio button checked state
private Boolean changeGroup = false;

private Context mCtx;
private ConfAccount mConfAccount;

//user checked id
private int mCheckedId = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_conference_main);

initView(); 
}

private void initView() {

mCtx = this;

mRgConfFunctionModule1 = (RadioGroup) findViewById(R.id.rg_function_moudle_1);
mRgConfFunctionModule2 = (RadioGroup) findViewById(R.id.rg_function_moudle_2);

mRgConfFunctionModule1.setOnCheckedChangeListener(this);
mRgConfFunctionModule2.setOnCheckedChangeListener(this);

long id = getIntent().getLongExtra(Constant.KEY_OF_CONF_ACCOUNT_ID, -1L);

mConfAccount = AppClass.getInstance().getAccountById(id);

if (!Util.isEmpty(mConfAccount)) {

setTitleName(mConfAccount.getConfAccountName());
}
}

@Override
protected void onResume() {
super.onResume();
}


@Override
protected void onDestroy() {
super.onDestroy();

}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

if (group != null && checkedId > -1 && changeGroup == false){

   if(group == mRgConfFunctionModule1){
   
     changeGroup = true;
     mRgConfFunctionModule2.clearCheck();
     changeGroup = false;
   }else if(group == mRgConfFunctionModule2){
   
     changeGroup = true;
     mRgConfFunctionModule1.clearCheck();
     changeGroup = false;
   }
   
   mCheckedId = checkedId;
 }
}

public void addParticipant() {

Util.startActivity(mCtx, AddParticipantActivity.class);
}

public void onRadioClick(View v) {

int id = v.getId();

switch (mCheckedId) {


case R.id.rb_open_hf:
//Util.shortToast(this, "hf");

break;
case R.id.rb_self_mute:
Util.shortToast(this, "self mute");
break;
case R.id.rb_mute_all:
Util.shortToast(this, "mute all");
break;
case R.id.rb_call_name:
Util.shortToast(this, "call name");
break;
case R.id.rb_record:
Util.shortToast(this, "record");
break;
case R.id.rb_add_participant:
// Util.shortToast(this, "add");
addParticipant();
break;
case R.id.rb_lock:
Util.shortToast(this, "lock");
break;
case R.id.rb_other_function:
Util.shortToast(this, "other");
break;
}
}

}



xml文件

<?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:background="@color/white"
    android:orientation="vertical" >


    <include
        android:id="@+id/title"
        layout="@layout/title_layout" />
  
    <RadioGroup
        android:id="@+id/rg_function_moudle_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dp"
        android:background="@color/white"
        android:orientation="horizontal" 
        android:>


        <RadioButton
            android:id="@+id/rb_open_hf"
            style="@style/RadioButtonStyle"
            android:drawableTop="@android:drawable/btn_radio"
            android:text="@string/conf_open_hf" />


        <RadioButton
            android:id="@+id/rb_self_mute"
            style="@style/RadioButtonStyle"
            android:drawableTop="@android:drawable/btn_radio"
            android:text="@string/conf_mute_self" />


        <RadioButton
            android:id="@+id/rb_mute_all"
            style="@style/RadioButtonStyle"
            android:drawableTop="@android:drawable/btn_radio"
            android:text="@string/conf_mute_all" />


        <RadioButton
            android:id="@+id/rb_call_name"
            style="@style/RadioButtonStyle"
            android:drawableTop="@android:drawable/btn_radio"
            android:text="@string/conf_call_name" />


        <RadioButton
            android:id="@+id/rb_record"
            style="@style/RadioButtonStyle"
            android:drawableTop="@android:drawable/btn_radio"
            android:text="@string/conf_record" />
    </RadioGroup>


    <RadioGroup
        android:id="@+id/rg_function_moudle_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="15dp"
        android:background="@color/white"
        android:orientation="horizontal" >


        <RadioButton
            android:id="@+id/rb_add_participant"
            style="@style/RadioButtonStyle"
            android:drawableTop="@android:drawable/btn_radio"
            android:text="@string/conf_add_participant" />


        <RadioButton
            android:id="@+id/rb_lock"
            style="@style/RadioButtonStyle"
            android:drawableTop="@android:drawable/btn_radio"
            android:text="@string/conf_lock" />


        <RadioButton
            android:id="@+id/rb_other_function"
            style="@style/RadioButtonStyle"
            android:drawableTop="@android:drawable/btn_radio"
            android:text="@string/conf_other_function" />
    </RadioGroup>


    <ListView
        android:id="@+id/lv_participant"
        style="@style/Normal_List"
        android:layout_height="0dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:background="@color/listview_bg_color"
        android:footerDividersEnabled="false" >
    </ListView>


</LinearLayout>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值