CheckBox复选框用法,先看效果图
选择点击按钮后,会在标题栏显示选中的项。
main.xml代码:
01
<CheckBox android:id="@+id/box1"
02
android:text="我是BOX1"
03
android:layout_width="fill_parent"
04
android:layout_height="wrap_content"
05
/>
06
<CheckBox android:id="@+id/box2"
07
android:text="我是BOX2"
08
android:layout_width="fill_parent"
09
android:layout_height="wrap_content"
10
/>
11
<Button android:id="@+id/b1"
12
android:layout_width="wrap_content"
13
android:layout_height="wrap_content"
14
android:text="按我"
15
/>
主程序代码:
01
package com.pocketdigi;
02
03
import android.app.Activity;
04
import android.os.Bundle;
05
import android.view.View;
06
import android.view.View.OnClickListener;
07
import android.widget.Button;
08
import android.widget.CheckBox;
09
10
public class main extends Activity {
11
/** Called when the activity is first created. */
12
@Override
13
public void onCreate(Bundle savedInstanceState) {
14
super.onCreate(savedInstanceState);
15
setContentView(R.layout.main);
16
17
boxDemo();
18
19
}
20
CheckBox box1;
21
CheckBox box2;
22
private void boxDemo(){
23
box1=(CheckBox)findViewById(R.id.box1);
24
box2=(CheckBox)findViewById(R.id.box2);
25
Button b1=(Button)findViewById(R.id.b1);
26
b1.setOnClickListener(Dialog);
27
}
28
private Button.OnClickListener Dialog=new OnClickListener(){
29
30
@Override
31
public void onClick(View v) {
32
// TODO Auto-generated method stub
33
String s="";
34
if(box1.isChecked()){
35
s+="box1,";
36
}
37
if(box2.isChecked()){
38
s+="box2";
39
}
40
setTitle(s);
41
}
42
43
};
44
}