Java 将侦听器添加到包含CheckedTextView和button的自定义listview

Java 将侦听器添加到包含CheckedTextView和button的自定义listview,java,android,android-studio,listener,Java,Android,Android Studio,Listener,这是我的行布局(rowlayout.xml) 在rowlayout.xml中,将android:genderantfocusability=“blocksDescendants”添加到根节点(在本例中为RelativeLayout) rowlayout.xml中的CheckedTextViewview和按钮都是可聚焦的,这会阻止ListView项获得焦点 有关更多信息,请参阅rowlayout.xml中的,将安卓:genderantfocusability=“blocksDescendants”

这是我的行布局(rowlayout.xml)


在rowlayout.xml中,将android:genderantfocusability=“blocksDescendants”添加到根节点(在本例中为RelativeLayout)

rowlayout.xml中的CheckedTextViewview按钮都是可聚焦的,这会阻止ListView项获得焦点


有关更多信息,请参阅rowlayout.xml中的,将安卓:genderantfocusability=“blocksDescendants”添加到根节点(本例中为RelativeLayout)

rowlayout.xml中的CheckedTextViewview按钮都是可聚焦的,这会阻止ListView项获得焦点


有关更多信息,请参阅

谢谢您的回答我添加了一个日志以查看我的结果日志.d(“工作条件”、“stringSelectedItems:+stringSelectedItems”);可以选择结果,但在布局上可绘制的结果仍然存在unchecked@testerNew你在问另一个问题。不管怎样,这让我很困惑,你到底想点击哪个项目。CheckedTextView,或包含CheckedTextView和按钮的列表视图项?如果要响应CheckedTextView上的click事件,必须在其上指定click listener。但是我没有在你的代码中找到这样的逻辑。谢谢你的回答,我添加了一个日志来查看我的结果日志。d(“工作条件”,“stringSelectedItems:+stringSelectedItems”);可以选择结果,但在布局上可绘制的结果仍然存在unchecked@testerNew你在问另一个问题。不管怎样,这让我很困惑,你到底想点击哪个项目。CheckedTextView,或包含CheckedTextView和按钮的列表视图项?如果要响应CheckedTextView上的click事件,必须在其上指定click listener。但是我在你的代码中没有发现这样的逻辑。
<?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="wrap_content">

    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/checkedTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"

        android:gravity="center_vertical"
        android:paddingRight="16dp"
        android:text="CheckedTextView" />

    <Button
        android:id="@+id/telque"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_toRightOf="@id/checkedTextView"
        android:text="telque" />


</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.doctorbeingwell.doctorbeingwell.createfilepack.WorkConditions">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:onClick="passconditions"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"
        android:id="@+id/linearLayout">

        <ListView
            android:id="@+id/checkabalelist"
            android:layout_width="match_parent"
            android:layout_height="491dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_weight="0.2"/>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>
public class WorkConditions extends Activity {
  ArrayList<String> selectedItems = new ArrayList<>();
    ListView checkablelist;
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_condition);


        checkablelist = findViewById(R.id.checkabalelist);
        checkablelist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        String[] items = {"étudiant",
                "chomeur",
                "retraité",
                "travail à domicile",
                "travail au bureau"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.checkedTextView, items);
        checkablelist.setAdapter(adapter);
        CheckedTextView checkedTextView= findViewById(R.id.checkedTextView);
        checkablelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                String selectedItem = ((CheckedTextView) view).getText().toString();
                if (selectedItems.contains(selectedItem)) {
                    selectedItems.remove(selectedItem);
                } else selectedItems.add(selectedItem);
            }
        });
String selectedItem = ((CheckedTextView) view).getText().toString();