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: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>
MainActivity.java
package com.example.helloword; import java.text.BreakIterator; import android.R.bool;
import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.renderscript.Script.KernelID;
import android.test.IsolatedContext;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView; public class MainActivity extends Activity {
private CheckBox cbJava, cbPython;
private TextView tvCheckedValue; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.cbJava = (CheckBox) this.findViewById(R.id.cbJava);
this.cbPython = (CheckBox) this.findViewById(R.id.cbPython);
this.tvCheckedValue = (TextView) this.findViewById(R.id.tvCheckedValue); CheckBoxOnCheckedChangeListener listener = new CheckBoxOnCheckedChangeListener(); this.cbJava.setOnCheckedChangeListener(listener);
this.cbPython.setOnCheckedChangeListener(listener); } private StringBuffer stringBuffer; private class CheckBoxOnCheckedChangeListener implements
CompoundButton.OnCheckedChangeListener {
private String checkJava = cbJava.getText().toString() + " ";
private String checkPython = cbPython.getText().toString() + " ";
private StringBuffer stringBuffer = new StringBuffer(); @Override
public void onCheckedChanged(CompoundButton compoundButton,
boolean ischecked) {
switch (compoundButton.getId()) {
case R.id.cbJava:
Log.i("info", "operator " + cbJava.getText());
break;
case R.id.cbPython:
Log.i("info", "operator " + cbPython.getText());
break;
default:
break;
} if (compoundButton == cbJava) {
changeValue(ischecked, checkJava);
} else if (compoundButton == cbPython) {
changeValue(ischecked, checkPython);
} tvCheckedValue.setText(stringBuffer.toString());
} private void changeValue(boolean ischecked, String checkValue) {
int start = stringBuffer.indexOf(checkValue);
int end = start + checkValue.length();
if (ischecked) {
if (start == -1) {
stringBuffer.append(checkValue);
}
} else {
if (start != -1) {
stringBuffer.delete(start, end);
}
}
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// 当点击回退时,弹出该窗口(也就相当于关闭操作)
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this).setTitle("是否退出?")
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
}).setNegativeButton("取消", null).show();
return true;
}
return super.onKeyUp(keyCode, event);
}
}