【问题标题】:CheckedTextView doesn't respond to clickCheckedTextView 没有响应点击
【发布时间】:2011-10-18 12:09:47
【问题描述】:

我有一个用 CheckedTextViews 动态填充的列表视图。列表视图设置为多选模式。我正在使用 OnItemClickListener 来响应对我的列表视图的点击。我还用 CheckedTextView 的布局制作了一个 XML 文件(实际上它只是标准 android.R.layout.simple_list_item_multiple_choice 的副本)。所以在这种情况下一切正常:当我单击列表视图中的一个项目时,相应的checkedtextview 会被选中。但是当我尝试使用以下布局时,checkedtextview 没有响应点击并且仍然未选中。

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_height="wrap_content"   
    android:layout_width="wrap_content"  
    android:orientation="vertical"  
    android:padding="5px">
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical"
        android:drawableLeft="@drawable/checkbox_selector"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:background="#00ffffff"
        android:textSize="15sp"
        android:singleLine="true"/>
</LinearLayout>    

我猜是因为 CheckedTextViews 被放入 LinearLayout 并且它们没有收到来自 Listview 列表项的点击事件。

【问题讨论】:

    标签: android android-layout checked checkedtextview


    【解决方案1】:

    我猜不一样。如果您查看source code of simple_list_item_multiple_choice.xml,您会看到android:checkMark 属性设置为?android:attr/listChoiceIndicatorMultiple。这就是 CheckedTextView 在内部用来绘制处于任何状态的复选框的内容,正如您可以从 its source code 看到的那样。

    但是你的布局中CheckedTextView 的定义缺少这个属性。我会责怪这一点,而不是在LinearLayout 元素中使用CheckedTextView

    我错了,你猜对了。似乎自定义ListView 的行需要实现CheckableCheckedTextView 可以,但LinearLayout 没有。有关详细信息,请参阅另一个 StackOverflow 线程中的 this answer

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,所以我将布局更改为 android.R.layout.simple_list_item_multiple_choice,而不是使用带有 CheckedTextView 的自定义布局。

      【讨论】:

        【解决方案3】:

        即使他们没有收到 OnClickEvent,您也可以使用列表项大小相同的布尔 ArrayList 来维护这些复选框的状态。 Here, 我刚刚回答了类似的问题。我希望它会给你一个更好的主意。

        【讨论】:

          【解决方案4】:

          这个问题有两个简单的解决方案:

          1. 删除 LinearLayout 和仅用户的 CheckedTextView(是的,这是可能的)。所以布局的主要元素是可检查的,它会被标记。

          2. 如果您的 minSDK 为 11 以上,请自定义 android:checkMarke 并将状态设置为已激活。这是一个例子:

          示例代码:

          <?xml version="1.0" encoding="utf-8"?>
          <selector  xmlns:android="http://schemas.android.com/apk/res/android" >
             <item android:state_activated="true" android:drawable="@drawable/checkbox_selected"/>  
             <item android:state_activated="false" android:drawable="@drawable/checkbox_deselected" />  
             <item android:state_checked="false" android:drawable="@drawable/checkbox_deselected" />  
             <item android:state_checked="true" android:drawable="@drawable/checkbox_selected" /> 
          </selector>
          

          来源:http://www.jiahaoliuliu.com/2013/08/customizing-checkbox-in-android.html

          中层解决方案是自定义列表视图的onItemClickListener,如下代码所示: https://github.com/jiahaoliuliu/CustomizedListRow

          困难但正确的解决方案是 MarvinLab 提出的解决方案: http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/

          相关问题:What is the difference between the states selected, checked and activated in Android?

          【讨论】:

            【解决方案5】:

            此问题是由 attr singleLine = true 引起的。如果您删除它或替换为maxLines = 1,它将正常工作。实际上,当您单击该项目时会检查状态 - 我认为这是一个 Android 错误。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-06-12
              • 2019-06-18
              • 1970-01-01
              • 2012-05-26
              • 1970-01-01
              相关资源
              最近更新 更多