簡體   English   中英

當 RadioButton 單擊 Listview setOnItemClickListener 不起作用

[英]When RadioButton clicked Listview setOnItemClickListener does not work

當我點擊radioButtononClickListener的ListView沒有一個動作的響應。 需要做什么才能采取行動。 當我單擊radioButton上的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>

它只包含列表視圖代碼。 活動問題.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>

爪哇代碼。

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

實現 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;
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM