android listview中checkbox 的点击事件

我们经常遇见这样的事情,在listview的item中包含有textview和checkBox。我们既想获取listitem的点击事件,又想获取listitem中textview的点击事件和listitem中checkBox的点击事件,那么有没有办法实现呢?答案是肯定的,我们只需重新创建listview的适配器继承BaseAdpter就可以了。另外如果有checkBox或者imageview在内的话就必须设置它聚焦为false。
关键点:
1.listview的适配器要继承BaseAdpt
2.checkBox或者imageview在内的话就必须设置它聚焦为false。

?
代码片段,双击复制
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;
}}
}

主页面的xml布局代码:
?
代码片段,双击复制
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
<?xml version= "1.0" encoding= "utf-8" ?>
 
<LinearLayout xmlns:android=http: //schemas.android.com/apk/res/android
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
 
>
 
<ListView
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:padding= "5dip"
android:id= "@+id/ListView"
>
 
</ListView>
</LinearLayout>
 
listitem的xml布局文件:
<?xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout
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"
>
 
<TextView
android:text= "TextView01"
android:layout_width= "100px"
android:layout_height= "wrap_content"
android:textSize= "20dip"
android:gravity= "left"
android:id= "@+id/wishname" />
<CheckBox
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"
>
 
</CheckBox>
</LinearLayout>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值