zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }
    }

    运行效果

  • 相关阅读:
    virtmanager 的 internal error Cannot find suitable emulator for x86_64 错误
    django 判断mysql中的bit(1)
    eucalyptus volume 的一些创建流程以及理解
    将eucalyptus数据库更改为Mysql
    ftp虚拟用户添加
    通过shell读取mysql数据
    Java webservice
    axis2之webservice
    基础巩固(二) log4j的使用
    基础巩固(一)
  • 原文地址:https://www.cnblogs.com/Aming-/p/12322294.html
Copyright © 2011-2022 走看看