• UI组件之RadioButton


    ##RadioButton
    RadioButton是单选按钮,允许用户在一个组中选择一个选项。同一组中的单选按钮有互斥效果。

    ###RadioButton的特点
    1.RadioButton是圆形单选框;
    2.RadioGroup是个可以容纳多个RadioButton的容器;
    3.在RadioGroup中的RadioButton控件可以有多个,但同时有且仅有一个可以被选中。

    ###使用Demo

    在布局文件中定义RadioGroup
    在RadioGroup中添加RadioButton(至少两个)
    在Java代码中获取控件对象
    为对象添加监听器,实现OnCheckedChangeListener接口,(选择RadioGroup包下的那个);
    重写onCheckedChanged方法。

    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:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请选择性别" />
    
        <RadioGroup
            android:id="@+id/rg_sex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <RadioButton
                android:id="@+id/rb_Male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男" />
    
            <RadioButton
                android:id="@+id/rb_FeMale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女" />
        </RadioGroup>
    </LinearLayout>

    java文件

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    
    public class MainActivity extends AppCompatActivity{
        private RadioGroup rg;
        private RadioButton rb_Male, rb_Female;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_radiobutton);
            rg = (RadioGroup) findViewById(R.id.rg_sex);
            rb_Male = (RadioButton) findViewById(R.id.rb_Male);
            rb_Female = (RadioButton) findViewById(R.id.rb_FeMale);
            //注意是给RadioGroup绑定监视器
            rg.setOnCheckedChangeListener(new MyRadioButtonListener() ); 
        }
    
        class MyRadioButtonListener implements OnCheckedChangeListener {
    
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 选中状态改变时被触发
                switch (checkedId) {
                case R.id.rb_FeMale:
                    // 当用户选择女性时
                    Log.i("sex", "当前用户选择"+rb_Female.getText().toString());
                    break;
                case R.id.rb_Male:
                    // 当用户选择男性时
                    Log.i("sex", "当前用户选择"+rb_Male.getText().toString());
                    break;
                }
            }
        }
    }

    在实际Android开发中,通常可以使用RadioButton控件来实现应用的底部导航栏

    (1条消息) 使用radiobutton实现底部导航栏_duolaimila的博客-CSDN博客

  • 相关阅读:
    Beta版本冲刺第二天 12.6
    Beta版本冲刺第一天 12.5
    Linux学习(2)—— 图形化界面
    Linux学习(1)—— 虚拟机安装Linux系统
    IntelliJ IDEA使用
    spring+springmvc+hibernate 整合
    新的篇章
    软件工程实践总结作业——个人作业
    Beta版本冲刺———第七天
    Beta版本冲刺———第六天
  • 原文地址:https://www.cnblogs.com/ltw222/p/14903563.html
Copyright © 2020-2023  润新知