android radio(单选框) 两个单选框绑定

在程序中会用到监听事件,监听到一个单选框被点击后,另一个单选框也会被选上。当然,这两个点选框不是在一组中的。

同样,在MainActivity.java文件中:

package com.yx.radio;

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

public class MainActivity extends Activity {

 private RadioGroup radioGroup;
 private RadioButton femaleButton;
 private RadioButton maleButton;
 private RadioGroup radioGroup2;
 private RadioButton femaleButton2;
 private RadioButton maleButton2;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  radioGroup = (RadioGroup)findViewById(R.id.radioGroupId);
  femaleButton = (RadioButton)findViewById(R.id.femaleButtonId);
  maleButton = (RadioButton)findViewById(R.id.maleButtonId);
  
  radioGroup2 = (RadioGroup)findViewById(R.id.radioGroup2Id);
  femaleButton2 = (RadioButton)findViewById(R.id.femaleButton2Id);
  maleButton2 = (RadioButton)findViewById(R.id.maleButton2Id);
  RadioGroupListener rgl = new RadioGroupListener();
  radioGroup.setOnCheckedChangeListener(rgl);
  radioGroup2.setOnCheckedChangeListener(rgl);
 }
 
 class RadioGroupListener implements OnCheckedChangeListener{//设置监听,需要实现OnCheckedChangeListener接口(这个应该是内部类)
  @Override
  public void onCheckedChanged(RadioGroup arg0, int arg1) {
   if(arg1 == femaleButton.getId()){
    femaleButton2.setChecked(true);
   }
   else if(arg1 == maleButton.getId()){
    maleButton2.setChecked(true);
   }
   else if(arg1 == femaleButton2.getId()){
    femaleButton.setChecked(true);
   }
   else if(arg1 == maleButton2.getId()){
    maleButton.setChecked(true);
   }
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

在activity_main.xml文件中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <RadioGroup
        android:id="@+id/radioGroupId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/femaleButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="female"
            />
         <RadioButton
            android:id="@+id/maleButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="male"
            />
    </RadioGroup>
   
    <RadioGroup
        android:id="@+id/radioGroup2Id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/femaleButton2Id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="female2"
            />
         <RadioButton
            android:id="@+id/maleButton2Id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="male2"
            />
    </RadioGroup>

</LinearLayout>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值