我是靠谱客的博主 细腻酒窝,最近开发中收集的这篇文章主要介绍【Android -- UI 开发】RadioButton/RadioGroup/CheckBox 的基本使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、RaidoButton

1. 效果图
rb

2. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="14dp"
        android:text="@string/tv_desc"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RadioGroup
        android:id="@+id/rg_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="53dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >
        <RadioButton
            android:id="@+id/rb_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rb_male"/>

        <RadioButton
            android:id="@+id/rb_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rb_female"/>
    </RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>

3. MainActivity.java

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.rg_sex)
    RadioGroup mSelectSex;
    @BindView(R.id.rb_male)
    RadioButton mMale;
    @BindView(R.id.rb_female)
    RadioButton mFemale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnCheckedChanged({R.id.rb_male,R.id.rb_female})
    public void onRadioCheck(CompoundButton view,boolean isChanged) {
        switch (view.getId()) {
            case R.id.rb_male:
                if (isChanged) {
                    Toast.makeText(this,"you select male",Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.rb_female:
                if (isChanged) {
                    Toast.makeText(this,"you select female",Toast.LENGTH_SHORT).show();
                }
                break;

            default:
                break;
        }
    }
}

二、RadioButton | RadioGroup

1. 布局文件

		<RadioGroup
            android:id="@+id/rg_detail"
            android:layout_width="160dp"
            android:layout_height="40dp"
            android:layout_marginTop="8dp"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/rb_basic_info"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:checked="true"
                android:text="基本信息"
                android:textColor="@drawable/sel_text"
                android:gravity="center"
                android:background="@drawable/rb_left"
                android:button="@null"/>

            <RadioButton
                android:id="@+id/rb_progress"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:text="处理进度"
                android:textColor="@drawable/sel_text"
                android:gravity="center"
                android:background="@drawable/rb_right"
                android:button="@null"/>
        </RadioGroup>

2. sel_text.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_checked="true"/>
    <item android:color="@color/white" android:state_pressed="true"/>
    <item android:color="@color/text_color"/>
</selector>

3. rb_left.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/rb_left_pre" android:state_checked="true"/>
    <item android:drawable="@drawable/rb_left_pre" android:state_pressed="true"/>
    <item android:drawable="@drawable/rb_left_nor"/>
</selector>

4. rb_left_nor.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:topLeftRadius="20dp"
        android:bottomLeftRadius="20dp" />
    <solid android:color="@color/white" />
</shape>

5. rb_left_pre.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:bottomLeftRadius="20dp"
        android:topLeftRadius="20dp" />
    <solid android:color="@color/blue" />
</shape>

6. 代码

public class OrderDetailActivity extends BaseActivity {
    @BindView(R.id.rg_detail)
    RadioGroup mDetail;
    @BindView(R.id.rb_basic_info)
    RadioButton mBasic;
    @BindView(R.id.rb_progress)
    RadioButton mProgress;

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;

    private BasicFragment mBasicFragment = HomeFragmentFactory.getInstance().getBasicFragment();
    private ProcessFragment mProcessFragment = HomeFragmentFactory.getInstance().getProcessFragment();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected void initView() {
        mTitleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                finish();
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });

        showFragment(mBasicFragment);
    }

    @OnCheckedChanged({R.id.rb_basic_info,R.id.rb_progress})
    public void onRadioCheck(CompoundButton view, boolean isChanged) {
        switch (view.getId()) {
            case R.id.rb_basic_info:
                if (isChanged) {
                    showFragment(mBasicFragment);
                }
                break;

            case R.id.rb_progress:
                if (isChanged) {
                    showFragment(mProcessFragment);
                }
                break;

            default:
                break;
        }
    }

    /**
     * 切换fragment
     *
     * @param targetFragment
     * @return
     */
    private void showFragment(Fragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_content, targetFragment, targetFragment.getClass().getName());
        transaction.commit();
    }


}

三、CheckBox

1. 效果图
cb

2. main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="14dp"
        android:text="@string/tv_desc"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/cb_basketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="58dp"
        android:text="@string/cb_basketball"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/cb_football"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cb_football"
        app:layout_constraintStart_toStartOf="@+id/cb_basketball"
        app:layout_constraintTop_toBottomOf="@+id/cb_basketball" />

    <CheckBox
        android:id="@+id/cb_pingpong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cb_pingpong"
        app:layout_constraintStart_toStartOf="@+id/cb_football"
        app:layout_constraintTop_toBottomOf="@+id/cb_football" />

    <CheckBox
        android:id="@+id/cb_badminton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cb_badminton"
        app:layout_constraintStart_toStartOf="@+id/cb_pingpong"
        app:layout_constraintTop_toBottomOf="@+id/cb_pingpong" />

    <Button
        android:id="@+id/btn_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:onClick="show"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

3. MainActivity.java

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.cb_basketball)
    CheckBox mBasketball;
    @BindView(R.id.cb_football)
    CheckBox mFootBall;
    @BindView(R.id.cb_pingpong)
    CheckBox mPingpong;
    @BindView(R.id.cb_badminton)
    CheckBox mBadminton;

    private Map<String, String> like = new HashMap();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnCheckedChanged({R.id.cb_basketball,R.id.cb_football,R.id.cb_pingpong,R.id.cb_badminton})
    public void onRadioCheck(CompoundButton view,boolean isCheck) {
        switch (view.getId()) {
            case R.id.cb_basketball:
                if (isCheck) {
                    like.put("basketball","篮球");
                }else {
                    like.remove("basketball");
                }
                break;

            case R.id.cb_football:
                if (isCheck) {
                    like.put("football","足球");
                }else {
                    like.remove("football");
                }
                break;

            case R.id.cb_pingpong:
                if (isCheck) {
                    like.put("pingpong","乒乓球");
                }else {
                    like.remove("pingpong");
                }
                break;

            case R.id.cb_badminton:
                if (isCheck) {
                    like.put("badminton","羽毛球");
                }else {
                    like.remove("badminton");
                }
                break;

            default:
                break;
        }
    }

    /**
     * 输出总的爱好
     *
     * @param v
     */
    public void show(View v) {
        // 循环读取爱好
        StringBuilder sb = new StringBuilder();
        String ret = "这个人的爱好是:";

        if (like.size() == 0) {
            String no = "这个人完全没有爱好!";
            Toast.makeText(MainActivity.this, no, Toast.LENGTH_SHORT).show();
        } else {
            sb.append(ret);
            for (String key : like.keySet()) {
                sb.append(like.get(key) + "t");
            }
            Toast.makeText(MainActivity.this, sb, Toast.LENGTH_SHORT).show();
        }

    }
}

最后

以上就是细腻酒窝为你收集整理的【Android -- UI 开发】RadioButton/RadioGroup/CheckBox 的基本使用的全部内容,希望文章能够帮你解决【Android -- UI 开发】RadioButton/RadioGroup/CheckBox 的基本使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部