RadioGroup setOnCheckedChangeListener 不适用于 RadioButton setOn



我有一个RadioGroup,里面有RadioButtons。我的问题是当我检查单选按钮侦听器不起作用时。当我删除单选按钮触摸侦听器事件时,它将起作用。

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检查它是否有效。 尝试后发布行为。

最新更新