• Android studio——RadioButton


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

    这个控件可以由非选中状态通过点击事件转为选中状态,但是不能通过点击实现逆向的状态转换,一个默认样式RadioButton控件的非选中和选中状态如下:

    其组成和CheckBox一样,我们同样可以分别对其中的字体和Button进行设置,实现达到和CheckBox一样的效果。

    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;
                }
            }
        }
    }

    运行效果

  • 相关阅读:
    B1001 害死人不偿命的(3n+1)猜想 (15 分)
    A1050 String Subtraction (20 分)
    A1041 Be Unique (20 分)
    B1047 编程团体赛 (20 分)
    B1043 输出PATest (20 分)
    B1042 字符统计 (20 分)
    B1038 统计同成绩学生 (20 分)
    VB计算符号
    vs2008写代码的时候不能输入中文,sogou和google输入法都没有用
    如何彻底关闭Windows7自动更新
  • 原文地址:https://www.cnblogs.com/Aming-/p/12322294.html
Copyright © 2020-2023  润新知