美文网首页
Android 多个RadioGroup实现单选功能

Android 多个RadioGroup实现单选功能

作者: 朝阳夕晖蜂蜜 | 来源:发表于2019-08-28 16:24 被阅读0次

1、实现单选的形式有很多种,可以使用列表形式,也可以使用RadioGroup,CheckBox等等,今天介绍一种多个GadioGroup嵌套GadioButton实现单选功能,直接上代码,作为参考

xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Main6Activity">

    <RadioGroup
        android:id="@+id/rg_ball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_basketball"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="篮球"/>

        <RadioButton
            android:id="@+id/rb_football"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="足球"/>

        <RadioButton
            android:id="@+id/rb_volleyball"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="排球"/>
    </RadioGroup>

    <RadioGroup
        android:id="@+id/rg_sport"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_swimming"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="游泳"/>

        <RadioButton
            android:id="@+id/rb_running"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="跑步"/>

        <RadioButton
            android:id="@+id/rb_yoga"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="瑜伽"/>
    </RadioGroup>
    <TextView
        android:id="@+id/tv_content"
        android:text="你选择的是:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

具体代码:

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup mRgBall;
    private RadioButton mRbBasketball;
    private RadioButton mRbFootball;
    private RadioButton mRbVolleyball;
    private RadioGroup mRgSport;
    private RadioButton mRbSwimming;
    private RadioButton mRbRunning;
    private RadioButton mRbYoga;
    private TextView mTvContent;



    private boolean isSelect = false;
    private String mGetContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        mRgBall = (RadioGroup) findViewById(R.id.rg_ball);
        mRbBasketball = (RadioButton) findViewById(R.id.rb_basketball);
        mRbFootball = (RadioButton) findViewById(R.id.rb_football);
        mRbVolleyball = (RadioButton) findViewById(R.id.rb_volleyball);
        mRgSport = (RadioGroup) findViewById(R.id.rg_sport);
        mRbSwimming = (RadioButton) findViewById(R.id.rb_swimming);
        mRbRunning = (RadioButton) findViewById(R.id.rb_running);
        mRbYoga = (RadioButton) findViewById(R.id.rb_yoga);
        mTvContent = (TextView) findViewById(R.id.tv_content);
        //初始化默认选中,没有默认选项就删除这部分
        mRbFootball.setChecked(true);
        mTvContent.setText("你选择的运动是:" + mRbFootball.getText());
        initListener();
    }

    private void initListener() {
        mRgBall.setOnCheckedChangeListener(this);
        mRgSport.setOnCheckedChangeListener(this);
        
       
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.rg_ball:
                selected(group, checkedId, mRgSport);
                break;
            case R.id.rg_sport:
                selected(group, checkedId, mRgBall);
                break;
        }
    }

    private void selected(RadioGroup group, int checkedId, RadioGroup radioGroup) {
        if (!isSelect) {
            isSelect = true;
            radioGroup.clearCheck();
            isSelect = false;
            RadioButton rb = group.findViewById(checkedId);
            if (rb.isChecked()) {
                mGetContent = (String) rb.getText();
                mTvContent.setText("你选择的运动是:" + mGetContent);
            }
        }
    }
}
1566980636(1).jpg

简单吧

相关文章

网友评论

      本文标题:Android 多个RadioGroup实现单选功能

      本文链接:https://www.haomeiwen.com/subject/nakmectx.html