zoukankan      html  css  js  c++  java
  • Android开发笔记(七)——CheckBox

    RadioButton可以实现在一组控件中单选的功能,CheckBox可以实现复选的功能。
    首先依旧是新建一个用于演示CheckBox的activity CheckBoxActivity ,此时 AndroidMainfest.xml 中会自动添加如下声明:

    <activity android:name=".CheckBoxActivity"></activity>
    

    之后在 activity_main.xml 中添加一个Button用来进行RadioButton控件的展示:

    <Button
        android:id="@+id/btn_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CheckBox"
        android:textAllCaps="false"
        />
    

    接下来在 MainActivity.java 中声明这个控件:

    private Button mBtnCheckBox;
    

    之后要在 MainActivity.java 中的 onCreate 函数中使用 findViewById 找到该button:

    mBtnCheckBox=findViewById(R.id.btn_radiobutton);
    

    setListener() 中设置监听事件:

    mBtnCheckBox.setOnClickListener(onClick);
    

    之后在 onClick 中添加:

    case R.id.btn_checkbox:
        //跳转到CheckBox演示界面
        intent=new Intent(MainActivity.this, CheckBoxActivity.class);
        break;
    

    具体可以看上次的笔记。
    之后打开布局文件 activity_check_box.xml 进行布局:

    常用属性

    和之前的内容差不多,下面是一个布局的例子:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp"
        >
    
       <TextView
           android:id="@+id/tv_title"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="你会哪些移动开发?"
           android:textSize="20sp"
           android:textColor="#000000"
           android:layout_marginBottom="20dp"
           />
    
       <CheckBox
            android:id="@+id/cb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android"
            android:textSize="20sp"
            android:layout_below="@id/tv_title"
            />
    
       <CheckBox
            android:id="@+id/cb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="IOS"
            android:textSize="20sp"
            android:layout_below="@id/cb_1"
            />
    
       <CheckBox
            android:id="@+id/cb_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="H5"
            android:textSize="20sp"
            android:layout_below="@id/cb_2"
            />
    
       <CheckBox
            android:id="@+id/cb_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="其他"
            android:textSize="20sp"
            android:layout_below="@id/cb_3"
            />
    </RelativeLayout>
    

    效果:

    自定义样式

    通常实例开发中都是采用自定义的样式:
    同样先进行布局:

       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:orientation="vertical"
           android:layout_below="@id/cb_4"
           android:layout_marginTop="20dp"
           >
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="你的兴趣爱好有哪些?"
               android:textSize="20sp"
               android:textColor="#000000"
               />
    
           <CheckBox
               android:id="@+id/cb_5"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="唱歌"
               android:textSize="20sp"
               android:layout_marginTop="20dp"
               />
    
           <CheckBox
               android:id="@+id/cb_6"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="绘画"
               android:textSize="20sp"
               />
    
           <CheckBox
               android:id="@+id/cb_7"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="其他"
               android:textSize="20sp"
               />
       </LinearLayout>
    

    把准备好的样式图片放置到 drawable-xxhdpi 中,再新建一个 drawable resource file

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

    背景选择器准备好之后,返回布局文件加入属性:

    android:button="@drawable/bg_checkbox"
    android:paddingLeft="10dp"
    

    其中 paddingLeft 是添加内边距。
    效果:

    监听事件

    CheckBoxActivity.java 中首先声明:

    private CheckBox mCb5,mCb6,mCb7;
    

    之后在 onCreate 函数中找到这几个当选中状态进行变化时想要有监听事件的控件:

    mCb5=findViewById(R.id.cb_5);
    mCb6=findViewById(R.id.cb_6);
    mCb7=findViewById(R.id.cb_7);
    

    之后再设置监听事件:

    mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Toast.makeText(CheckBoxActivity.this,isChecked?"选中":"未选中",Toast.LENGTH_SHORT).show();
        }
    });
    

    其他两个按钮设置方法一样。
    效果:

  • 相关阅读:
    OpenGL简介及编程入门
    大数阶乘 我的ACM的第一步!
    解释Windows7“上帝模式”的原理
    斯特林[striling]公式(求阶乘(n!)的位数)
    VC中借助内嵌资源实现Flash动画播放
    VC MFC 精品文章收集!
    这应该是UFC有史以来最快的KO记录了
    深以为然
    PS3欧洲延期!全球同步发售幻想破灭
    Windows Vista RC1 NVIDIA drivers include OpenGL ICD
  • 原文地址:https://www.cnblogs.com/yangdd/p/13300348.html
Copyright © 2011-2022 走看看