• 一手遮天 Android


    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - view(选择类): CheckBox 基础

    示例如下:

    /view/selection/CheckBoxDemo1.java

    /**
     * CheckBox - 复选框控件
     *     setChecked(boolean checked) - 设置当前复选框的选中状态
     *     isChecked() - 获取当前复选框的选中状态
     *     setOnCheckedChangeListener(OnCheckedChangeListener listener) - 复选框的选中状态发生变化时的回调
     *         手动使选中状态发生变化或用程序使选中状态发生变化都会触发此事件
     *         如果不希望用程序使选中状态发生变化时会触发事件,则请使用 setOnClickListener()
     */
    
    package com.webabcd.androiddemo.view.selection;
    
    import androidx.appcompat.app.AppCompatActivity;
    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 com.webabcd.androiddemo.R;
    
    public class CheckBoxDemo1 extends AppCompatActivity implements View.OnClickListener, CheckBox.OnCheckedChangeListener {
    
        private CheckBox _checkBox1;
        private CheckBox _checkBox2;
        private Button _button1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_selection_checkboxdemo1);
    
            _checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
            _checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
            _button1 = (Button) findViewById(R.id.button1);
    
            // 将 _checkBox2 设置为选中状态
            _checkBox2.setChecked(true);
    
            // 复选框的选中状态发生变化时的回调(手动使其发生变化或用程序使其发生变化都会触发此事件)
            _checkBox1.setOnCheckedChangeListener(this);
    
            // 复选框被单击时的回调(有的时候会用到这个,而不用 setOnCheckedChangeListener,比如你不希望用程序使选中状态发生变化时会触发事件)
            _checkBox2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(CheckBoxDemo1.this, _checkBox2.getText() + " " + _checkBox2.isChecked(), Toast.LENGTH_SHORT).show();
                }
            });
    
            _button1.setOnClickListener(this);
        }
    
        // 任何时候都可以通过此方法来获取复选框的选中状态
        @Override
        public void onClick(View v) {
            String result = "选中项 ";
            if (_checkBox1.isChecked()) {
                result += _checkBox1.getText() + " ";
            }
            if (_checkBox2.isChecked()) {
                result += _checkBox2.getText() + " ";
            }
    
            Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        }
    
        // 复选框的选中状态发生变化时的回调
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Toast.makeText(this, buttonView.getText() + " " + isChecked, Toast.LENGTH_SHORT).show();
        }
    }
    
    

    /layout/activity_view_selection_checkboxdemo1.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">
    
        <!--
            CheckBox - 复选框控件
                checked - 是否被选中
        -->
    
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="checkBox1" />
    
        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="checkBox2" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="submit" />
    
    </LinearLayout>
    
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    HBase Cassandra比较
    重新认识HBase,Cassandra列存储——本质是还是行存储,只是可以动态改变列(每行对应的数据字段)数量而已,当心不是parquet
    HBase底层存储原理——我靠,和cassandra本质上没有区别啊!都是kv 列存储,只是一个是p2p另一个是集中式而已!
    Cassandra 数据模型设计,根据你的查询来制定设计——反范式设计本质:空间换时间
    【LeetCode】【Python解决问题的方法】Best Time to Buy and Sell Stock II
    LVM逻辑卷管理命令
    Java引进和应用的包装类
    Android 4.0新组件:GridLayout详细说明
    【剑指offer】打印单列表从尾部到头部
    原因以及如何避免产生僵尸进程
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_selection_CheckBoxDemo1.html
Copyright © 2020-2023  润新知