0

テキストビューと右側にチェックボックスがあるリストビューがあるアクティビティがあります。リストビューの項目をクリックしたときにチェックボックスをチェックしたい。(チェックボックスにはありません)。Androidがメッセージを1つずつチェックして削除するために使用するもののように。

誰でもこれを解決するのを手伝ってもらえますか? 私はオフにしました

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);
    }
});
4

3 に答える 3

1

チェックボックスに追加android:clickable="false"します。

状況によっては、代わりにCheckedTextViewを使用し、を使用ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)して複数選択を有効にすることをお勧めします。

AbsoluteSizeSpan/RelativeSizeSpan + SpannableStringBuilder1つのTextViewに異なるテキストサイズを実装するのに役立ちます。

于 2012-11-23T06:25:30.227 に答える
1

私の答えは遅すぎますが、完全に機能します。

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から変数値を取得したい場合は、これをチェックしてください

于 2014-09-13T06:04:57.290 に答える
0

以下は、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();
            }
        });
于 2012-11-23T06:20:11.100 に答える