Android學習筆記(七)——CheckBox

2020-10-28 16:01:07

Android小白,初次學習,筆記自用,敬請指正~😛
有幫助記得一鍵三連呀(點贊收藏關注)😁

一,學習內容:

1,基本使用<CheckBox/>
2,設定監聽事件
2,如何自定義選擇樣式:先將自己想要使用的圖示放到drawable資料夾下,然後通過background屬性進行參照即可。
此內容比較簡單,具體見下面完整程式碼(新增註釋為了方便理解,執行時將xml中的//註釋去掉)

二,效果演示:

在這裡插入圖片描述

三,完整程式碼:

1,activity_main.xml

<Button
        android:id="@+id/btn_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CheckBox"
        android:textAllCaps="false"/> //文字是否設定大寫

2,MainActivity.java

 	mBtnCheckBox = findViewById(R.id.btn_checkbox);
        mBtnCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳轉到RadioButton演示介面
                Intent intent = new Intent(MainActivity.this,mBtnCheckBoxActivity.class);
                startActivity(intent);
            }
        });

3,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="15dp">

    <TextView
        android:id="@+id/tv_tittle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你會哪些移動開發?"
        android:textSize="20sp"
        android:textColor="#000000"/>
    <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_tittle"
        android:layout_marginTop="10dp"/>
    <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"/>
    <LinearLayout
        android:layout_width="match_parent"
        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="10dp"
            android:button="@drawable/bg_checkbox"/>
        <CheckBox
            android:id="@+id/cb_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="遊戲"
            android:textSize="20sp"
            android:button="@drawable/bg_checkbox"/>
        <CheckBox
            android:id="@+id/cb_7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="做飯"
            android:textSize="20sp"
            android:button="@drawable/bg_checkbox"/>
    </LinearLayout>



</RelativeLayout>

4,CheckBoxActivity.java

package com.example.learning;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class CheckBoxActivity extends AppCompatActivity {

    private CheckBox mCb5,mCb6,mCb7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);

        mCb5 = findViewById(R.id.cb_5);
        mCb6 = findViewById(R.id.cb_6);
        mCb7 = findViewById(R.id.cb_7);
		//設定監聽事件,選中或取消選中時進行文字提示(Toast.makeText())
        mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(CheckBoxActivity.this,isChecked?"5選中":"5未選中",Toast.LENGTH_SHORT).show();
            }
        });
        mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(CheckBoxActivity.this,isChecked?"6選中":"6未選中",Toast.LENGTH_SHORT).show();
            }
        });
        mCb7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(CheckBoxActivity.this,isChecked?"7未選中":"7未選中", Toast.LENGTH_SHORT).show();
            }
        });

    }
}

5,bg_checkbox.xml

<?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_unchecked"/>  //自定義設定未選中時的顯示樣式

    <item android:state_checked="true"
        android:drawable="@drawable/icon_checked"/>  //自定義設定選中時的顯示樣式
</selector>

相關筆記直通車:

Android學習筆記(一)——LinearLayout
Android學習筆記(二)——RelativeLayout
Android學習筆記(三)——TextView
Android學習筆記(四)——Button
Android學習筆記(五)——EditView
Android學習筆記(六)——RadioButton