RadioButton的使用(三个button,实现互斥,一次只能选中一个)

效果:只能选中一个radiobutton


实现的方法:

1.首先我们要将三个radiobutton的layout设置成可以点击的,然后我们将这三个radio设置成不可以点击的,我们通过点击layout来实现,radiobutton的选中,通过设置               android:focusable="true"    android:clickable="true"属性来实现

2.我们在每一个layout中设置一个tag,用于管理当前点击的是哪个radiobutton

   例子:                 <RelativeLayout
                        android:id="@+id/rl_alipay"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="@dimen/largePadding"
                        android:paddingBottom="@dimen/largePadding"
                        android:background="@drawable/selector_list_item"
                        android:focusable="true"
                        android:clickable="true"
                        android:tag="alipay">

3.我们在activity中分别实例化上面radiobutton和layout的对象

pyRadioButton= (RadioButton) findViewById(R.id.rb_alipay);
wxRadioButton= (RadioButton) findViewById(R.id.rb_webchat);
baiduRadioButton= (RadioButton) findViewById(R.id.rb_bd);
pyRelativeLayout= (RelativeLayout) findViewById(R.id.rl_alipay);
wxRelaticeLayout= (RelativeLayout) findViewById(R.id.rl_wechat);
bdRelativeLayout= (RelativeLayout) findViewById(R.id.rl_bd);

4.创建爱一个hasmap用于存储三个radiobutton对象,并且给他们赋予一个值,值为下面的值

private HashMap<String,RadioButton> channels = new HashMap<>(3);
channels.put(CHANNEL_ALIPAY,pyRadioButton);
channels.put(CHANNEL_WECHAT,wxRadioButton);
channels.put(CHANNEL_BFB,baiduRadioButton);

/**
 * 银联支付渠道
 */
private static final String CHANNEL_UPACP = "upacp";
/**
 * 微信支付渠道
 */
private static final String CHANNEL_WECHAT = "wx";
/**
 * 支付支付渠道
 */
private static final String CHANNEL_ALIPAY = "alipay";
/**
 * 百度支付渠道
 */
private static final String CHANNEL_BFB = "bfb";
/**
 * 京东支付渠道
 */
private static final String CHANNEL_JDPAY_WAP = "jdpay_wap";


6.设置三个layout布局的点击事件


pyRelativeLayout.setOnClickListener(this);
wxRelaticeLayout.setOnClickListener(this);
bdRelativeLayout.setOnClickListener(this);


/**
 * radiobutton的点击事件,让每个按钮互斥
 * @param v
 */
@Override
public void onClick(View v) {
selectPayChannle(v.getTag().toString());

}



调用一个方法,这个方法就是根据,当前点击的是哪个layout,来实现radiobutton的互斥,选中

public void selectPayChannle(String paychannel){


    for (Map.Entry<String,RadioButton> entry:channels.entrySet()){

    //    payChannel = paychannel;
        RadioButton rb = entry.getValue();
        if(entry.getKey().equals(paychannel)){

            boolean isCheck = rb.isChecked();
            rb.setChecked(!isCheck);

        }
        else
            rb.setChecked(false);
    }


}

流程大概是这样:遍历hasmap中的对象,然后抓取每个对象的值是否和点击layout传进去的值相同,相同取反,每点击的点击,点击的设置成不点击状态



********************以上所有的步骤已经完成


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值