01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
public
class
Main
extends
Activity {
private
ListView list;
private
ListAdapter listadapter;
private
String[] arr;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
arr=
new
String[]{
"111"
,
"222"
,
"333"
};
// 绑定Layout里面的ListView
list = (ListView) findViewById(R.id.ListView);
listadapter =
new
ListAdapter();
// 添加并且显示
list.setAdapter(listadapter);
// 添加点击事件
list.setOnItemClickListener(
new
OnItemClickListener() {
public
void
onItemClick(AdapterView> parent, View view,
int
position,
long
id) {
// TODO Auto-generated method stub
//这里放Item点击事件
Toast.makeText(Main.
this
,
"Item点击事件"
,Toast.LENGTH_SHORT).show();
}
});
}
private
class
ListAdapter
extends
BaseAdapter {
public
int
getCount() {
// TODO Auto-generated method stub
return
arr.length;
}
public
Object getItem(
int
position) {
// TODO Auto-generated method stub
return
position;
}
public
long
getItemId(
int
position) {
// TODO Auto-generated method stub
return
position;
}
public
View getView(
int
position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
//获取布局文件
if
(view ==
null
) {
view = getLayoutInflater().inflate(R.layout.listview,
null
);
}
//获取控件
TextView name = (TextView) view.findViewById(R.id.wishname);
CheckBox ck = (CheckBox) view.findViewById(R.id.checkBox1);
if
(arr!=
null
)
{
name.setText(arr[position]);
name.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
//这里放点击改变事件
Toast.makeText(Main.
this
,
"TextView点击事件"
,Toast.LENGTH_SHORT).show();
}
});
ck.setOnCheckedChangeListener(
new
OnCheckedChangeListener() {
public
void
onCheckedChanged(CompoundButton buttonView,
boolean
isChecked) {
// TODO Auto-generated method stub
//这里放点击改变事件
Toast.makeText(Main.
this
,
"CheckBox点击事件"
,Toast.LENGTH_SHORT).show();
}
});
}
return
view;
}}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
android:orientation=
"vertical"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
>
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:padding=
"5dip"
android:id=
"@+id/ListView"
>
listitem的xml布局文件:
xmlns:android=http:
//schemas.android.com/apk/res/android
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:padding=
"10dip"
android:id=
"@+id/linear"
>
android:text=
"TextView01"
android:layout_width=
"100px"
android:layout_height=
"wrap_content"
android:textSize=
"20dip"
android:gravity=
"left"
android:id=
"@+id/wishname"
/>
android:layout_width=
"40px"
android:layout_height=
"wrap_content"
android:layout_alignParentRight=
"true"
android:layout_marginLeft=
"140dp"
android:focusable=
"false"
//加这句的原因是因为checkBox的点击事件优先级高于listview的点击事件,所以要屏蔽之
android:id=
"@+id/checkBox1"
>
|