Android:CheckBox控件
阿新 • • 發佈:2017-11-01
set inter ini str es2017 kernel xtend inflater bre
1)ChexkBox繼承自CompoundButton組件;
2)isChecked()--確定是否選中;setChecked(bool checked)--設置選中或取消選中;
3)監聽事件:CompoundButton.OnCheckedChangeListener
使用checkbox,並實現監聽測試:
1)效果:
2)源代碼:
res\layout\activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:toolsView Code="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <CheckBox android:id="@+id/cbJava" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="27dp" android:text="Java Runtime 9.0" /> <TextView android:id="@+id/tvCheckedValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/cbJava" android:layout_below="@+id/cbPython" android:layout_marginLeft="14dp" android:layout_marginTop="54dp" android:text="" /> <CheckBox android:id="@+id/cbPython" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/cbJava" android:layout_below="@+id/cbJava" android:layout_marginTop="22dp" android:text="Python runtime2.7" /> </RelativeLayout>
MainActivity.java
1 package com.example.helloword; 2 3 import java.text.BreakIterator; 4 5 import android.R.bool; 6 import android.R.string; 7 import android.app.Activity; 8 import android.app.AlertDialog; 9 import android.content.DialogInterface; 10 import android.content.DialogInterface.OnClickListener; 11 import android.os.Bundle; 12 import android.renderscript.Script.KernelID; 13 import android.test.IsolatedContext; 14 import android.util.Log; 15 import android.view.KeyEvent; 16 import android.view.Menu; 17 import android.view.View; 18 import android.widget.Button; 19 import android.widget.CheckBox; 20 import android.widget.CompoundButton; 21 import android.widget.EditText; 22 import android.widget.ImageView; 23 import android.widget.RadioButton; 24 import android.widget.RadioGroup; 25 import android.widget.TextView; 26 27 public class MainActivity extends Activity { 28 private CheckBox cbJava, cbPython; 29 private TextView tvCheckedValue; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 this.cbJava = (CheckBox) this.findViewById(R.id.cbJava); 36 this.cbPython = (CheckBox) this.findViewById(R.id.cbPython); 37 this.tvCheckedValue = (TextView) this.findViewById(R.id.tvCheckedValue); 38 39 CheckBoxOnCheckedChangeListener listener = new CheckBoxOnCheckedChangeListener(); 40 41 this.cbJava.setOnCheckedChangeListener(listener); 42 this.cbPython.setOnCheckedChangeListener(listener); 43 44 } 45 46 private StringBuffer stringBuffer; 47 48 private class CheckBoxOnCheckedChangeListener implements 49 CompoundButton.OnCheckedChangeListener { 50 private String checkJava = cbJava.getText().toString() + " "; 51 private String checkPython = cbPython.getText().toString() + " "; 52 private StringBuffer stringBuffer = new StringBuffer(); 53 54 @Override 55 public void onCheckedChanged(CompoundButton compoundButton, 56 boolean ischecked) { 57 switch (compoundButton.getId()) { 58 case R.id.cbJava: 59 Log.i("info", "operator " + cbJava.getText()); 60 break; 61 case R.id.cbPython: 62 Log.i("info", "operator " + cbPython.getText()); 63 break; 64 default: 65 break; 66 } 67 68 if (compoundButton == cbJava) { 69 changeValue(ischecked, checkJava); 70 } else if (compoundButton == cbPython) { 71 changeValue(ischecked, checkPython); 72 } 73 74 tvCheckedValue.setText(stringBuffer.toString()); 75 } 76 77 private void changeValue(boolean ischecked, String checkValue) { 78 int start = stringBuffer.indexOf(checkValue); 79 int end = start + checkValue.length(); 80 if (ischecked) { 81 if (start == -1) { 82 stringBuffer.append(checkValue); 83 } 84 } else { 85 if (start != -1) { 86 stringBuffer.delete(start, end); 87 } 88 } 89 } 90 } 91 92 @Override 93 public boolean onCreateOptionsMenu(Menu menu) { 94 // Inflate the menu; this adds items to the action bar if it is present. 95 getMenuInflater().inflate(R.menu.main, menu); 96 return true; 97 } 98 99 @Override 100 public boolean onKeyUp(int keyCode, KeyEvent event) { 101 // 當點擊回退時,彈出該窗口(也就相當於關閉操作) 102 if (keyCode == KeyEvent.KEYCODE_BACK) { 103 new AlertDialog.Builder(this).setTitle("是否退出?") 104 .setPositiveButton("確定", new OnClickListener() { 105 @Override 106 public void onClick(DialogInterface arg0, int arg1) { 107 finish(); 108 } 109 }).setNegativeButton("取消", null).show(); 110 return true; 111 } 112 return super.onKeyUp(keyCode, event); 113 } 114 }
Android:CheckBox控件