• 2.06RadioButton(单选按钮)&Checkbox(复选框)-UI组件-Android


    TOC

    RadioButton(单选按钮)&Checkbox(复选框)-UI组件-Android

    RadioButton (单选按钮)

    需要把RadioButton放到RadioGroup按钮组中, 从而实现单选功能!

    可以为外层RadioGroup设置orientation属性然后设置RadioButton的排列方式,是竖直还是水平

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请选择性别"
            android:textSize="23dp"
            />
        <RadioGroup
            android:id="@+id/radioGroupId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"> <!--横向排列horizontal 纵向 vertical-->
            <RadioButton
                android:id="@+id/btnMan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"
                android:checked="true"/> <!--设置默认被选中-->
            <RadioButton
                android:id="@+id/btnWoman"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"/>
        </RadioGroup>
        <Button
            android:id="@+id/btnpost"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交"/>
    </LinearLayout>

    属性

    横向:android:orientation="horizontal"

    纵向:android:orientation="vertical"

    方法

    getChildCount:获取包含的按扭的数量

    getChildAt:获取第i个单选按扭

    isChecked( ):判断按钮是否选中

    获得选中的值

    方法1:设置一个事件监听器 setOnCheckChangeListener

            RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroupId);
            //第一种获得单选按钮值的方法
            //为radioGroup设置一个监听器:setOnCheckedChanged()
            radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton radbtn = (RadioButton) findViewById(checkedId);
                    Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show();
                }
            });

    方法2:通过单击其他按钮获取选中单选按钮的值

    点击按扭的时候,获取选中的值

            Button btnchange = (Button) findViewById(R.id.btnpostId);
            final RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroupId);
    //为radioGroup设置一个监听器:setOnCheckedChanged()
            btnchange.setOnClickListener(new View.OnClickListener() {//点击事件
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < radgroup.getChildCount(); i++) {
                        RadioButton rd = (RadioButton) radgroup.getChildAt(i);
                        if (rd.isChecked()) {//判断是否被选中了
                            Toast.makeText(getApplicationContext(), "点击提交按钮,获取你选择的是:" + rd.getText(), Toast.LENGTH_LONG).show();//getApplicationContext()==类.this
                            break;
                        }
                    }
                }
            });

    CheckBox (复选框)

    <?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:paddingLeft="10dp">
    
        <CheckBox
            android:id="@+id/cb_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="香蕉"
            android:checked="true"
            android:button="@drawable/checkbox"
            android:textSize="30sp" />
    
        <CheckBox
            android:id="@+id/cb_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:padding="40dp"
            android:text="西瓜"
            android:checked="false"
            android:textSize="30sp" />
    
        <CheckBox
            android:id="@+id/cb_three"
            style="@style/MyCheckBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="苹果"
            android:textSize="30sp" />
    
        <Button
            android:id="@+id/btn_send"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交"
            android:textSize="30sp" />
    </LinearLayout>

    设置监听事件

    package com.ttit.helloworld;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    public class CheckBoxAcitivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    
        private CheckBox cb_one;
        private CheckBox cb_two;
        private CheckBox cb_three;
        private Button btn_send;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.checkbox_test);
            //获取组件对象
            cb_one = (CheckBox) findViewById(R.id.cb_one);
            cb_two = (CheckBox) findViewById(R.id.cb_two);
            cb_three = (CheckBox) findViewById(R.id.cb_three);
            btn_send = (Button) findViewById(R.id.btn_send);
            //设置监听事件
            cb_one.setOnCheckedChangeListener(this);
            cb_two.setOnCheckedChangeListener(this);
            cb_three.setOnCheckedChangeListener(this);
            btn_send.setOnClickListener(this);
    
        }
    
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {//CheckBox继承了CompoundButton,可以使用这个监听
            String s = compoundButton.getText().toString();
            if (compoundButton.isChecked()) {
                Toast.makeText(this, compoundButton.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        }
        //点击监听事件
        @Override
        public void onClick(View view) {
            String choose = "";
            if (cb_one.isChecked()) choose += cb_one.getText().toString() + "";
            if (cb_two.isChecked()) choose += cb_two.getText().toString() + "";
            if (cb_three.isChecked()) choose += cb_three.getText().toString() + "";
            Toast.makeText(this, choose, Toast.LENGTH_SHORT).show();
        }
    }

    自定义点击效果:

    1.定义点击的图形

    在drawable中定义

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:state_enabled="true"
            android:state_checked="true"
            android:drawable="@mipmap/checked"/>
        <item
            android:state_enabled="true"
            android:state_checked="false"
            android:drawable="@mipmap/uncheck" />
    </selector>

    2.引用

    方法1:直接使用android:button="@drawable/checkbox"

    方法2:将这个图形名设置为字符串,在style中引用

    res/values/styles.xml中设置

        <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
            <item name="android:button">@drawable/checkbox</item>
        </style>

    引用:style="@style/MyCheckBox"

    修改文字与选择框的距离

    android:background="@null"  <!--去掉左边距 -->
    android:paddingLeft="20dp"
  • 相关阅读:
    坚持--从今天开始
    51系列单片机的精确延时的解释(文章如有问题之处,请劳烦指正,谢谢!) 可以看看采纳下。
    利用宏定义实现C++程序在Unix和Win32环境下的通用性
    [转]浅谈C++指针直接调用类成员函数
    类间调用inline函数的效率
    C++ inline函数与编译器设置
    GNU的makefile文件编写说明
    Windows Live Writer 2012 Test
    测试Windows Live Writer
    Mathematica学习笔记2
  • 原文地址:https://www.cnblogs.com/ziyue7575/p/ece245313672610c5969047bcbdaba78.html
Copyright © 2020-2023  润新知