java - RadioGroup setOnCheckedChangeListener 不适用于 RadioButton setOnTouchListener
问题描述
我有一个 RadioGroup,里面有 RadioButtons。我的问题是当我检查单选按钮侦听器不起作用时。当我删除单选按钮 touchlistener 事件时,它会起作用。
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 以获取事件。
仍然尝试return false
使用 onTouch 方法检查它是否有效。尝试后发布行为。
推荐阅读
- python - 通过使用日期时间约束计算不同值进行聚合
- c# - blazor Web 程序集服务对象引用未设置为对象的实例
- ruby - 如何隔离 Ruby 中的方法 - 多处理问题
- python - 如何从python中的字符串中只获取单词?
- java - 单个测试执行问题期间的多个弹簧上下文
- python - 使用 Tkinter() 的 BMI 计算器程序
- flutter - 状态变量未按预期返回值 Flutter
- python - Cv2 findChessboardCorners 找不到角点
- python - 结果:失败异常:OSError:[Errno 19] No such device: '/home/site/wwwroot' in Python Azure Function
- python - 如何以图像格式保存CNN模型的预测结果?