如何捕获ListView中的复选框事件
我在 ListView 中仅添加了复选框。
list_checkbox.xml:
<复选框 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="42px">
在java中;为了捕获列表上的事件,我使用了这个::
items1={" "," "," "," "}; //blank i.e. no text near checkbox
lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_checkbox, items1));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When clicked,put code here.....
}
});
但是现在在哪里以及如何捕获复选框事件? 请详细说明。
我是新手。
谢谢..
I have added only checkboxes in my ListView.
list_checkbox.xml:<?xml version="1.0" encoding="utf-8"?>
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="42px">
</CheckBox>
In java; to capture event on list i have used this::
items1={" "," "," "," "}; //blank i.e. no text near checkbox
lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_checkbox, items1));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When clicked,put code here.....
}
});
but now where and how to capture the checkbox event?
in detail plz.
i am a newbie.
thanx..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在 ListView 中放入可聚焦或可点击的视图,则您的 OnItemClickListener 将不会被调用。
默认情况下复选框是可单击的,因此在这种情况下设置 OnItemClickListener 不会执行任何操作。
在这种情况下,您可以做的是实现一个自定义适配器(只需扩展ArrayAdapter)并向您为ListView提供的每个View添加一个OnClickListener。
您可以在此处找到有关此主题的非常好的教程。整章都值得阅读,但您问题的答案在第 112 页“交互式行”。
另请参阅这个 Stack Overflow 问题。
If you put in the ListView either focusable or clickable Views then the your OnItemClickListener won't get called.
Checkbox is clickable by default so setting OnItemClickListener in this case won't do anything.
What you can do in this case is to implement a custom Adapter (just extend ArrayAdapter) and add an OnClickListener to every View you provide for the ListView.
You can find a very good tutorial on this topic here. It's worth reading the whole chapter but the answer to your question is on page 112, Interactive Rows.
Also look at this Stack Overflow question.