【问题标题】:OnItemClickListener was not work with the checkbox?OnItemClickListener 不能与复选框一起使用?
【发布时间】:2012-04-11 15:44:24
【问题描述】:

我有一个这样的项目布局,并使用项目选择器设置背景

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@drawable/itemselector"
android:orientation="horizontal" >
<CheckBox
    android:id="@+id/message_row_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/message_row_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold"
        android:textColor="@color/black" />

itemselector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
 android:state_pressed="true" 
 android:drawable="@color/yellow" />
<item 
 android:state_selected="true" 
 android:drawable="@color/green" />
<item 
 android:drawable="@color/white" />
</selector>

我有一个 ListView 将包含一些项目。然后我使用了 setOnItemClickListener() 但它不起作用。我发现如果我删除项目中的复选框,一切都会好的。

这里的复选框和侦听器之间有什么问题? 你能给我一些解决方案吗?

更新:这是监听器的代码

mainListView.setAdapter(messageAdapter);
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                        Message p = (Message) arg0.getItemAtPosition(arg2);
                        Toast.makeText(TarsiusActivity.this, p.getTitle(), Toast.LENGTH_LONG);
                        Log.i("Item Clicked", p.getTitle());
                    }
});

ps:我想让收件箱像 android 上的 gmail。每行都有一个复选框,如果用户想查看消息,可以单击项目

【问题讨论】:

标签: android checkbox onitemclicklistener


【解决方案1】:

使用setOnCheckedChangeListener 代替onItemClickListner 作为复选框

CheckBox check;
check=new CheckBox(this);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
    }
});

【讨论】:

  • 还有其他解决方案吗?我想让它看起来像android上的gmail。仍然有复选框,如果用户点击该项目,如果他们想阅读消息
【解决方案2】:

如果listView 中有按钮、ImageButton、Checkbox 等任何可点击项,则listView 的onItemClickListener 将不起作用。添加

mainListView.setItemsCanFocus(true);

参考ListView OnItemClickListener Not Responding?

【讨论】:

    【解决方案3】:

    您可以在 OnItemClickListener 方法中添加此代码:

    public void onItemClick(AdapterView parent, View view, int position, long id){
       CheckBox box = (CheckBox)view.findViewById(R.id.course_search_checkbox);
       box.setChecked(true);
    }
    

    【讨论】:

    • 感谢您的解决方案,但您是否在 android 上使用了 GMAIL? :)
    • 是的...在收件箱中,如果您单击复选框,则会有一些选项供您选择,但是如果您单击其余选项,它将打开消息..我想要什么;)
    • 首先,您必须在对齐为 parentbottom 的同一 xml 中创建选项视图。在您的活动的 onCreate 中,将整个选项视图的可见性设置为消失。接下来,当您单击复选框时,然后将选项视图的可见性设置为在您的 onItemClick 中可见。
    • 这不是正确的解决方案,因为在检查 listitem 后它不能删除复选框值
    【解决方案4】:

    最好的方法是为您的复选框设置以下属性:

      android:focusable="false"
      android:focusableInTouchMode="false"
    

    我遇到了同样的问题并做了这个。

    【讨论】:

    • 如果您禁用复选框,则工作!
    • 也为我工作。谢谢
    【解决方案5】:

    只需添加

    android:descendantFocusability="blocksDescendants"

    到列表项的顶级 LinearLayout。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      相关资源
      最近更新 更多