【问题标题】:Check box not getting checked after clicking listview单击列表视图后未选中复选框
【发布时间】:2014-09-13 14:12:08
【问题描述】:

我有一个活动,其中有一个列表视图,其中包含文本视图和右侧的复选框。我希望在单击列表视图项时选中复选框。 (不在复选框上)。就像android用来一一检查要删除的消息一样。

谁能帮我解决这个问题?我关机了

android:focusable="false

android:focusableInTouchMode="false" 

在复选框上。

下面是我的列表视图项 xml。

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="1."
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="18dp"
    android:layout_toRightOf="@+id/textView2"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="22dp"
    android:layout_marginTop="-15dp" 
    android:focusable="false"
    android:focusableInTouchMode="false"/>

这是我的代码:

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
        // TODO Auto-generated method stub
        if(checkbox.isChecked())
            checkbox.setChecked(false);

        else
            checkbox.setChecked(true);
    }
});

【问题讨论】:

  • 请发布一些代码(XML 布局)。
  • 请发布/分享代码(列表项的xml)!
  • 考虑使用CheckedTextView(如android.R.layout.simple_list_item_checked)或编写自定义可检查布局。
  • 将此属性指定给您的主 RelativeLayout android:descendantFocusability="blocksDescendants"
  • 你的列表视图在哪里?

标签: android android-listview android-checkbox


【解决方案1】:

android:clickable="false" 添加到复选框。

在您的情况下,最好使用 CheckedTextView,并使用 ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) 启用多选。

AbsoluteSizeSpan/RelativeSizeSpan + SpannableStringBuilder 可以帮助您在一个 TextView 中实现不同的文本大小。

【讨论】:

    【解决方案2】:

    我的回答为时已晚,但效果很好。

    list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
                CheckBox cb = (CheckBox) arg1.findViewById(R.id.checkBox1);
                  TextView tv = (TextView) arg1.findViewById(R.id.textView1);
                  cb.performClick(); //this will trigger the checkbox
                  //do here 
                  if(checkbox.isChecked())
                      checkbox.setChecked(false);
                  else
                      checkbox.setChecked(true);
            }
    });
    

    如果您想从 BaseAdapter 获取变量值以获取检查位置或值等,请检查 this

    【讨论】:

      【解决方案3】:

      下面是我修改后添加CheckedtextView的xml

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
      
      
              <CheckedTextView
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/checkedTextView"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:checkMark="?android:attr/listChoiceIndicatorMultiple"
                  android:gravity="center_vertical"
                  android:paddingLeft="6dip"
                  android:paddingRight="6dip"
                  android:textAppearance="?android:attr/textAppearanceMedium" />
      
      </RelativeLayout>
      

      下面是listAdapter的getView里面写的onClick方法

      CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
              chkBox.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v)
                  {
                      ((CheckedTextView) v).toggle();
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-29
        • 2019-04-02
        • 1970-01-01
        相关资源
        最近更新 更多