CheckBox復選框,繼承於Button,並且提供了可以選中的功能:
實現入下效果:
當點擊全選的時候,所有的郵件都被選中,取消時也一起取消;當點擊刪除時,需要提示用於是否已選擇要刪除的checkBox。
在activity_main.xml中的代碼入下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" 5 android:padding="16dp" > 6 7 <TextView 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:text="收件箱" /> 11 12 <CheckBox 13 android:id="@+id/check_all" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="全選" /> 17 18 <View 19 android:layout_width="match_parent" 20 android:layout_height="1px" 21 android:background="#ff0000" /> 22 23 <LinearLayout 24 android:id="@+id/email_layout" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:orientation="vertical" > 28 29 <CheckBox 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:text="郵件1" /> 33 34 <CheckBox 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="郵件2" /> 38 39 <CheckBox 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:text="郵件3" /> 43 44 <CheckBox 45 android:layout_width="wrap_content" 46 android:layout_height="wrap_content" 47 android:text="郵件4" /> 48 49 <CheckBox 50 android:layout_width="wrap_content" 51 android:layout_height="wrap_content" 52 android:text="郵件5" /> 53 </LinearLayout> 54 55 <Button 56 android:id="@+id/btn_delete" 57 android:layout_width="wrap_content" 58 android:layout_height="wrap_content" 59 android:layout_marginTop="5dp" 60 android:layout_gravity="right" 61 android:text="刪除" /> 62 63 </LinearLayout>
在MainActivity中的代碼入下:
1 public class MainActivity extends Activity { 2 3 private CheckBox deleteAllBox; 4 private LinearLayout emailLayout; 5 private Button deleteBtn; 6 private int num; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 13 // 獲取控件 14 deleteAllBox = (CheckBox) findViewById(R.id.check_all); 15 emailLayout = (LinearLayout) findViewById(R.id.email_layout); 16 deleteBtn = (Button) findViewById(R.id.btn_delete); 17 // 全選CheckBox添加監聽 18 deleteAllBox 19 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 20 21 @Override 22 public void onCheckedChanged(CompoundButton buttonView, 23 boolean isChecked) { 24 for (int i = 0; i < emailLayout.getChildCount(); i++) { 25 CheckBox checkBox = (CheckBox) emailLayout 26 .getChildAt(i); 27 checkBox.setChecked(isChecked); 28 } 29 } 30 }); 31 // 刪除按鈕添加監聽 32 deleteBtn.setOnClickListener(new View.OnClickListener() { 33 34 @Override 35 public void onClick(View v) { 36 37 for (int i = 0; i < emailLayout.getChildCount(); i++) { 38 CheckBox checkBox = (CheckBox) emailLayout.getChildAt(i); 39 if (checkBox.isChecked()) { 40 num++; 41 } 42 } 43 //Toast.makeText(MainActivity.this, "num--->"+num, Toast.LENGTH_LONG).show(); 44 if (num > 0) { 45 for (int i = 0; i < emailLayout.getChildCount(); i++) { 46 CheckBox checkBox = (CheckBox) emailLayout 47 .getChildAt(i); 48 if (checkBox.isChecked()) { 49 /* 50 * View.GONE不可見且不占據空間 View.VISIBLE正常可見 51 * View.INVISIBLE不可見但占據空間 52 */ 53 checkBox.setVisibility(View.GONE); 54 //移除 55 emailLayout.removeView(checkBox); 56 num--; 57 //Toast.makeText(MainActivity.this, "剩余--->"+emailLayout.getChildCount(), Toast.LENGTH_LONG).show(); 58 } 59 } 60 } else { 61 Toast.makeText(MainActivity.this, "尚未選擇", 62 Toast.LENGTH_SHORT).show(); 63 } 64 65 } 66 }); 67 68 } 69 70 }