checkbox 学习
1 package com.example.firstapp; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.os.Bundle; 6 import android.widget.CheckBox; 7 import android.widget.CompoundButton; 8 import android.widget.Toast; 9 10 public class CheckBoxActivity extends AppCompatActivity { 11 12 13 private CheckBox mCb5,mCb6; 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_check_box); 18 mCb5=findViewById(R.id.cb_5); 19 mCb6=findViewById(R.id.cb_6); 20 21 mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 22 @Override 23 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 24 Toast.makeText(CheckBoxActivity.this,isChecked?"5选中":"5未选中",Toast.LENGTH_SHORT).show(); 25 } 26 }); 27 28 mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 29 @Override 30 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 31 Toast.makeText(CheckBoxActivity.this,isChecked?"6选中":"6未选中",Toast.LENGTH_SHORT).show(); 32 } 33 }); 34 } 35 }