复选按钮和开关按钮代码如下:
1 <LinearLayout 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content"> 4 <CheckBox 5 android:layout_width="wrap_content" 6 android:layout_height="wrap_content" 7 android:text="范冰冰" 8 android:id="@+id/cb_1"/> 9 <CheckBox 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="章子怡" 13 android:id="@+id/cb_2" 14 android:checked="true"/> 15 </LinearLayout> 16 <LinearLayout 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content"> 19 <ToggleButton 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:textOn="开" 23 android:textOff="关" 24 android:id="@+id/tb_1"/> 25 <Switch 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:text="开关" 29 android:id="@+id/sw_1"/> 30 </LinearLayout>
java类的代码:
1 package com.hanqi.testapp2; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.widget.CheckBox; 6 import android.widget.CompoundButton; 7 import android.widget.RadioButton; 8 import android.widget.RadioGroup; 9 import android.widget.Switch; 10 import android.widget.Toast; 11 import android.widget.ToggleButton; 12 13 public class TestActivity1 extends AppCompatActivity { 14 15 CheckBox cb_1; 16 CheckBox cb_2; 17 ToggleButton tb_1; 18 Switch sw_1; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_test1); 23 cb_1 = (CheckBox)findViewById(R.id.cb_1); 24 cb_2 = (CheckBox)findViewById(R.id.cb_2); 25 tb_1 = (ToggleButton)findViewById(R.id.tb_1); 26 sw_1 = (Switch)findViewById(R.id.sw_1); 27 28 tb_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 29 @Override 30 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 31 Toast.makeText(TestActivity1.this, "ToggleButton开关状态 = "+(isChecked?"开":"关"), Toast.LENGTH_SHORT).show(); //学习java时的小技巧——三元运算符 32 } 33 }); 34 35 sw_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 36 @Override 37 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 38 Toast.makeText(TestActivity1.this, "Switch开关状态 = "+(isChecked?"开":"关"), Toast.LENGTH_SHORT).show(); 39 } 40 }); 41 42 //监听器的实例 43 CB_OnCheckedChangeListener cb1 = new CB_OnCheckedChangeListener(); 44 //监听器的绑定 45 cb_1.setOnCheckedChangeListener(cb1); 46 cb_2.setOnCheckedChangeListener(cb1); 47 //公共的复选按钮的监听器 48 class CB_OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener 49 { 50 @Override 51 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 52 CheckBox cb = (CheckBox)buttonView; 53 String str = cb.getText().toString(); 54 if(isChecked) 55 { 56 Toast.makeText(TestActivity1.this, str+"被选中", Toast.LENGTH_SHORT).show(); 57 } 58 else 59 { 60 Toast.makeText(TestActivity1.this, str+"被取消选中", Toast.LENGTH_SHORT).show(); 61 } 62 } 63 } 64 }
时间: 2024-10-15 21:56:43