当RadioButton单击Listview时,setOnItemClickListener不起作用

时间:2017-10-29 17:58:51

标签: java android listview radio-button onitemclicklistener

当我点击radioButton时,Listview的onClickListener不会对操作做出响应。采取行动需要做些什么。当我在Listview上点击radioButton时,我想要采取行动。但点击时listviewListener会听到其他组件。有类似的问题,但我找不到正确的答案来克服这个问题。

question_item.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical"
        android:padding="@dimen/inner_size">

        <TextView
            android:id="@+id/explanationText"
            style="@style/Font.Medium.18"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/inner_size"
            android:text="explanation"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/questionText"
            style="@style/Font.Medium.18"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/inner_size"
            android:text="question"
            android:textSize="18sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/questionImageView"
            android:layout_width="200dp"
            android:layout_height="125dp"
            android:layout_gravity="center" />

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/answerRadio1"
                style="@style/RadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:descendantFocusability="blocksDescendants"
                android:focusableInTouchMode="true"
                android:padding="3dp"
                android:text="a) choose" />


            <RadioButton
                android:id="@+id/answerRadio2"
                style="@style/RadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:descendantFocusability="blocksDescendants"
                android:focusableInTouchMode="true"
                android:padding="3dp"
                android:text="b) choose" />


            <RadioButton
                android:id="@+id/answerRadio3"
                style="@style/RadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="true"
                android:padding="3dp"
                android:text="c) choose" />


            <RadioButton
                android:id="@+id/answerRadio4"
                style="@style/RadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:focusableInTouchMode="true"
                android:padding="3dp"
                android:text="d) choose" />

        </RadioGroup>

    </LinearLayout>

它只包含listview代码。 的 activity_question.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/activityQuestionList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/gray"
        android:dividerHeight="@dimen/listview_divider_height" />

</LinearLayout>

java代码。

activityQuestionList = (ListView) findViewById(R.id.activityQuestionList);
        activityQuestionList.setItemsCanFocus(false);

        questionAdapter = new QuestionAdapter(questions, this);
        activityQuestionList.setAdapter(questionAdapter);


        activityQuestionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (view.getId()) {
                    case R.id.answerRadio1:
                        answerA(i);
                        Toast.makeText(view.getContext(), "index : " + i, Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.answerRadio2:
                        answerB(i);
                        break;
                    case R.id.answerRadio3:
                        answerC(i);
                        break;
                    case R.id.answerRadio4:
                        answerD(i);
                        break;
                }

                Toast.makeText(view.getContext(), "index : " + i, Toast.LENGTH_SHORT).show();
            }
        });

1 个答案:

答案 0 :(得分:0)

实现setOnCheckedChangeListener方法

private void checkListeners() {
    radioButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked)
                condition1=1;
            else
                condition1=0;
        }
    });

    radioButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked)
                condition2=1;
            else condition2=0;
        }
    });
}