簡體   English   中英

RadioGroup setOnCheckedChangeListener不適用於RadioButton setOnTouchListener

[英]RadioGroup setOnCheckedChangeListener not work with RadioButton setOnTouchListener

我有一個帶有RadioButtons的RadioGroup。 我的問題是當我檢查單選按鈕偵聽器將無法工作時。 當我刪除單選按鈕觸摸偵聽器事件時,它將起作用。

final RadioGroup RGroup = (RadioGroup) view.findViewById(R.id.RGroup);

    RGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton checkedRadioButton = (RadioButton) group.findViewById(checkedId);
            boolean isChecked = checkedRadioButton.isChecked();
            if (isChecked)
            {
                checkedRadioButton.setButtonDrawable(R.drawable.ic_check_black_24dp);
            }
        }
    });

當我刪除以下代碼時,此代碼有效

 button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                    int colorFrom = v.getResources().getColor(android.R.color.transparent);
                    int colorTo = v.getResources().getColor(R.color.colorAnswer);
                    ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
                    colorAnimation.setDuration(1000); // milliseconds
                    colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                        @Override
                        public void onAnimationUpdate(ValueAnimator animator) {
                            button.setBackgroundColor((int) animator.getAnimatedValue());
                        }

                    });
                    colorAnimation.start();
                return true;
            }
        });

單選按鈕的布局

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_below="@id/slide_question"
    android:layout_marginTop="@dimen/pad_7dp"
    android:id="@+id/RGroup">

<android.support.v7.widget.AppCompatRadioButton
    android:id="@+id/radioButton1"
    android:layout_width="match_parent"
    android:layout_marginBottom="@dimen/pad_5dp"
    android:padding="@dimen/pad_10dp"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    app:fontFamily="@font/ubuntu"
    android:textColor="@color/colorBlackText"
    app:buttonTint="@color/colorPrimary"
    android:text="RadioButton" />
<android.support.v7.widget.AppCompatRadioButton
    android:id="@+id/radioButton2"
    android:layout_width="match_parent"
    android:layout_marginBottom="@dimen/pad_5dp"
    android:padding="@dimen/pad_7dp"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    app:fontFamily="@font/ubuntu"
    android:textColor="@color/colorBlackText"
    app:buttonTint="@color/colorPrimary"
    android:text="RadioButton" />

</RadioGroup>

也許聽眾有沖突

是的,這是因為聽眾發生沖突。 那時,當您應用touchListener時,它將停止組的checkChangeListener來獲取事件。

仍然嘗試通過onTouch方法return false來檢查其是否正常工作。 嘗試后發布行為。

暫無
暫無

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

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