【发布时间】:2018-04-10 17:39:07
【问题描述】:
当我单击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>
它只包含列表视图代码。 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();
}
});
【问题讨论】:
-
问题结构?
-
我有列表视图。当我点击 textview 组件时,响应 onClickItemListener。但是当我点击单选按钮时,我无法在 onClickItemListener 上收听它。
标签: java android listview radio-button onitemclicklistener