zoukankan      html  css  js  c++  java
  • 单选按钮(RadioButton)

    一:RadioButton的相关属性:

    1.Activity

    //单选按钮
    public class RadioButtonActivity extends Activity {
    
        private Context context;
        private RadioGroup sexRadioGroup;
        private RadioButton maleRadioButton;
        private RadioButton femaleRadioButton;
        private RadioButton sexRadioButton;
        private Button submitButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.radio_button);
    
            init();
            addAction();
    
        }
    
        private void init() {
            context = this;
            sexRadioGroup = (RadioGroup) findViewById(R.id.sexRadioGroupId);
            maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButtonId);
            femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButtonId);
            sexRadioButton = (RadioButton) findViewById(R.id.sexRadioButtonId);
            submitButton = (Button) findViewById(R.id.submitButtonId);
        }
    
        private void addAction() {
            // sexRadioGroup.setOnClickListener(l),setOnClickListener:点击的时候触发
            // setOnCheckedChangeListener:状态改变的时候触发
            // 两者都能实现对CheckBox的状态改变的监听,但一般情况下,用的更多的是setOnCheckedChangeListener。
            //因为,当CheckBox的状态不是通过点击事件改变,而是通过其他的方式改变时,比如setCheck(),setOnClickListener无法完成此种情况下的监听。
            //OnCheckChangedListener监听CheckBox的状态,无论来自你的onClick事件还是其他。
            sexRadioGroup
                    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            switch (checkedId) {
                            case R.id.maleRadioButtonId:
                                Toast.makeText(context, "你选择了"男".",
                                        Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.femaleRadioButtonId:
                                Toast.makeText(context, "你选择了"女".",
                                        Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.sexRadioButtonId:
                                Toast.makeText(context, "你选择了"人妖".",
                                        Toast.LENGTH_SHORT).show();
                                break;
                            default:
                                break;
                            }
                        }
                    });
    
            submitButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    String sex = null;
                    if (maleRadioButton.isChecked()) {
                        sex = "男";
                    }
                    if (femaleRadioButton.isChecked()) {
                        sex = "女";
                    }
                    if (sexRadioButton.isChecked()) {
                        sex = "人妖";
                    }
                    Toast.makeText(context, "你的性别是:" + sex, Toast.LENGTH_SHORT)
                            .show();
                }
            });
        }
    
    }

    2.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"
        android:padding="5dp" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别:"
                android:textSize="20sp" />
    
            <RadioGroup
                android:id="@+id/sexRadioGroupId"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
    <!--        android:button="@null" 可以自定义图片-->
                <RadioButton
                    android:id="@+id/maleRadioButtonId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="男" />
    
                <RadioButton
                    android:id="@+id/femaleRadioButtonId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="女" />
                
                <RadioButton
                    android:id="@+id/sexRadioButtonId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="人妖" />
            </RadioGroup>
        </LinearLayout>
    
        <Button
            android:id="@+id/submitButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="确定"
            android:textSize="20sp" />
    
    </LinearLayout>

    3.效果图展示:

  • 相关阅读:
    5G应用前景广泛 不止是下电影更快,还能做这些事……
    新思科技Chekib:AI芯片架构创新面临四大挑战
    融通人工智能指数(LOF)净值下跌1.08% 请保持关注
    ThinkPHP学习 volist标签高级应用之多重嵌套循环、隔行变色(转)
    (document).height()、$(document).scrollTop()
    jquery获取元素到屏幕底的可视距离
    jquery获取元素到页面顶部距离
    CSS实现背景透明而背景上的文字不透明完美解决
    怎么阻止事件的冒泡过程?
    CSS从大图中抠取小图完整教程(background-position应用) (转)
  • 原文地址:https://www.cnblogs.com/wuziyue/p/5372220.html
Copyright © 2011-2022 走看看