<CheckBoxButton.java>
1: package com.android.checkboxbutton;
2:
3: import android.app.Activity;
4: import android.os.Bundle;
5: import android.view.View;
6: import android.view.View.OnClickListener;
7: import android.widget.Button;
8: import android.widget.CheckBox;
9: import android.widget.CompoundButton;
10: import android.widget.CompoundButton.OnCheckedChangeListener;
11:
12: public class CheckBoxButton extends Activity {
13: /** Called when the activity is first created. */
14: private CheckBox chk;
15: private Button b1, b2;
16:
17: @Override
18: public void onCreate(Bundle savedInstanceState) {
19: super.onCreate(savedInstanceState);
20: setContentView(R.layout.main);
21:
22: findViews();
23: setListeners();
24: }
25:
26:
27: private void findViews(){
28: chk = (CheckBox)findViewById(R.id.chk);
29: b1 = (Button)findViewById(R.id.btnReverseCheck);
30: b2 = (Button)findViewById(R.id.btnExit);
31: }
32:
33:
34: private void setListeners(){
35: chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
36: public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
37: if(isChecked)
38: {
39: chk.setText("目前狀態 : 已勾選");
40: }
41: else
42: {
43: chk.setText("目前狀態 : 尚未勾選");
44: }
45: }
46: });
47:
48: b1.setOnClickListener(new OnClickListener(){
49: public void onClick(View arg0) {
50: chk.setChecked(!chk.isChecked());
51: }
52: });
53:
54: b2.setOnClickListener(new OnClickListener(){
55: public void onClick(View arg0) {
56: android.os.Process.killProcess(android.os.Process.myPid());
57: }
58: });
59: }
60:
61: }
<main.xml>
1: <?xml version="1.0" encoding="utf-8"?>
2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"3: android:layout_width="fill_parent"4: android:layout_height="fill_parent"5: android:orientation="vertical" >6:
7: <CheckBox
8: android:id="@+id/chk"9: android:layout_width="fill_parent"10: android:layout_height="wrap_content"11: android:text="@string/strCheckbox" />12:
13: <Button
14: android:id="@+id/btnReverseCheck"15: android:layout_width="fill_parent"16: android:layout_height="wrap_content"17: android:text="@string/strReverseCheck" />18:
19: <Button
20: android:id="@+id/btnExit"21: android:layout_width="wrap_content"22: android:layout_height="wrap_content"23: android:text="@string/strExit" />24:
25: </LinearLayout>
<strings.xml>
1: <?xml version="1.0" encoding="utf-8"?>2: <resources>
3:
4: <string name="hello">Hello World, CheckBoxButton!</string>5: <string name="app_name">CheckBoxButton</string>6: <string name="strCheckbox">目前狀態:尚未勾選</string>7: <string name="strButton">反向勾選</string>8: <string name="strExit">離開</string>9: <string name="strReverseCheck">反向勾選</string>10:
11: </resources>
沒有留言:
張貼留言