Android(CheckBox控件)

犀渠玉剑良家子,白马金羁侠少年。这篇文章主要讲述Android:CheckBox控件相关的知识,希望能为你提供帮助。
1)ChexkBox继承自CompoundButton组件;
2)isChecked()--确定是否选中;setChecked(bool checked)--设置选中或取消选中;
3)监听事件:CompoundButton.OnCheckedChangeListener
使用checkbox,并实现监听测试:
1)效果:

Android(CheckBox控件)

文章图片

Android(CheckBox控件)

文章图片

2)源代码:
res\\layout\\activity_main.xml
Android(CheckBox控件)

文章图片
Android(CheckBox控件)

文章图片
< RelativeLayout 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: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>

View CodeMainActivity.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 { 28private CheckBox cbJava, cbPython; 29private TextView tvCheckedValue; 30 31@Override 32protected void onCreate(Bundle savedInstanceState) { 33super.onCreate(savedInstanceState); 34setContentView(R.layout.activity_main); 35this.cbJava = (CheckBox) this.findViewById(R.id.cbJava); 36this.cbPython = (CheckBox) this.findViewById(R.id.cbPython); 37this.tvCheckedValue = https://www.songbingjia.com/android/(TextView) this.findViewById(R.id.tvCheckedValue); 38 39CheckBoxOnCheckedChangeListener listener = new CheckBoxOnCheckedChangeListener(); 40 41this.cbJava.setOnCheckedChangeListener(listener); 42this.cbPython.setOnCheckedChangeListener(listener); 43 44} 45 46private StringBuffer stringBuffer; 47 48private class CheckBoxOnCheckedChangeListener implements 49CompoundButton.OnCheckedChangeListener { 50private String checkJava = cbJava.getText().toString() +" "; 51private String checkPython = cbPython.getText().toString() + " "; 52private StringBuffer stringBuffer = new StringBuffer(); 53 54@Override 55public void onCheckedChanged(CompoundButton compoundButton, 56boolean ischecked) { 57switch (compoundButton.getId()) { 58case R.id.cbJava: 59Log.i("info", "operator " + cbJava.getText()); 60break; 61case R.id.cbPython: 62Log.i("info", "operator " + cbPython.getText()); 63break; 64default: 65break; 66} 67 68if (compoundButton == cbJava) { 69changeValue(ischecked, checkJava); 70} else if (compoundButton == cbPython) { 71changeValue(ischecked, checkPython); 72} 73 74tvCheckedValue.setText(stringBuffer.toString()); 75} 76 77private void changeValue(boolean ischecked, String checkValue) { 78int start = stringBuffer.indexOf(checkValue); 79int end = start + checkValue.length(); 80if (ischecked) { 81if (start == -1) { 82stringBuffer.append(checkValue); 83} 84} else { 85if (start != -1) { 86stringBuffer.delete(start, end); 87} 88} 89} 90} 91 92@Override 93public boolean onCreateOptionsMenu(Menu menu) { 94// Inflate the menu; this adds items to the action bar if it is present. 95getMenuInflater().inflate(R.menu.main, menu); 96return true; 97} 98 99@Override 100public boolean onKeyUp(int keyCode, KeyEvent event) { 101// 当点击回退时,弹出该窗口(也就相当于关闭操作) 102if (keyCode == KeyEvent.KEYCODE_BACK) { 103new AlertDialog.Builder(this).setTitle("是否退出?") 104.setPositiveButton("确定", new OnClickListener() { 105@Override 106public void onClick(DialogInterface arg0, int arg1) { 107finish(); 108} 109}).setNegativeButton("取消", null).show(); 110return true; 111} 112return super.onKeyUp(keyCode, event); 113} 114 }

【Android(CheckBox控件)】 

    推荐阅读