CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener
生活随笔
收集整理的這篇文章主要介紹了
CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<?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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.gby.s01_e09_checkbox.MainActivity"><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical" /><CheckBoxandroid:id="@+id/eatId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="吃飯" /><CheckBoxandroid:id="@+id/sleepId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="睡覺" /><CheckBoxandroid:id="@+id/dotaId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="dota" /><!--<Button--><!--android:id="@+id/bt_selectall"--><!--android:layout_width="wrap_content"--><!--android:layout_height="wrap_content"--><!--android:text="全選" />--><!--<Button--><!--android:id="@+id/bt_reverseSelection"--><!--android:layout_width="wrap_content"--><!--android:layout_height="wrap_content"--><!--android:text="反選" />--><!--<Button--><!--android:id="@+id/bt_deselectall"--><!--android:layout_width="wrap_content"--><!--android:layout_height="wrap_content"--><!--android:text="取消選擇" />--></LinearLayout>
MainActivity.java
package com.example.gby.s01_e09_checkbox;import android.support.v7.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.TextView;public class MainActivity extends AppCompatActivity {private CheckBox eatBox;private CheckBox sleepBox;private CheckBox dotaBox;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);eatBox = (CheckBox)findViewById(R.id.eatId);sleepBox = (CheckBox)findViewById(R.id.sleepId);dotaBox = (CheckBox)findViewById(R.id.dotaId);CheckBoxListener listener = new CheckBoxListener();//聲明實例化 監(jiān)聽器對象eatBox.setOnCheckedChangeListener(listener);//設置setOn監(jiān)聽器CheckedChangeListener,參數(shù)是監(jiān)聽器對象sleepBox.setOnCheckedChangeListener(listener);//不論哪個控件被點擊,都會調用onCheckedChanged()方法dotaBox.setOnCheckedChangeListener(listener);// OnBoxClickListener listener = new OnBoxClickListener();//聲明listener實例化OnClickListner的被實現(xiàn)對象OnBoxClickListener // eatBox.setOnClickListener(listener);//1個監(jiān)聽器可以綁定在多個控件上 // sleepBox.setOnClickListener(listener); // dotaBox.setOnClickListener(listener);} //OnCheckedChangeListener,CompoundButton的接口,可以理解為轉為CheckBox設計的class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (buttonView.getId() == R.id.eatId){//判斷被選中的是哪個CheckBox多選按鈕的IDSystem.out.println("eatBox");}else if (buttonView.getId() == R.id.sleepId){System.out.println("sleepBox");}else if (buttonView.getId() == R.id.dotaId){System.out.println("dotaBox");}if (isChecked){//判斷 選中狀態(tài) 的 布爾值booleanSystem.out.println("checked");}else {System.out.println("unchecked");}}}//OnClickListener的使用方法 // class OnBoxClickListener implements View.OnClickListener{@Override // public void onClick(View view) { // CheckBox box = (CheckBox)view;//向下轉型 //System.out.println("id-->"+view.getId());//打印view.id // if (box.getId() == R.id.eatId){//判斷是哪個checkBox的ID,并且打印相應的語句 // System.out.println("eatBox"); // // } // else if (box.getId() == R.id.sleepId){ // System.out.println("sleepBox"); // } // else if (box.getId() == R.id.dotaId){ // System.out.println("dotaBox"); // } // if ( box.isChecked();){//返回是否被選中狀態(tài) // System.out.println("checked"); // } // else{ // System.out.println("unchecked"); // } // System.out.println("Checkbox is clicked"); // } // } }總結
以上是生活随笔為你收集整理的CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UBUNTU完美运行TM,RTX,MSO
- 下一篇: 关于rtx 2009 远程控制插件 下载