如果您没有使用Custom View
显式覆盖的onPerformClick
,仅遵循Secko的回答就不会消除警告。
除了他的回答,做同样像类android.widget.Button
或者Button
你需要延伸目标视图一个简单的自定义视图。
范例:
自定义视图类:
public class UselessButton extends AppCompatButton {
public UselessButton(Context context) {
super(context);
}
public UselessButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UselessButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean performClick() {
return super.performClick();
}
}
XML:
<stackoverflow.onEarth.UselessButton
android:id="@+id/left"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/left"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/right"
app:layout_constraintVertical_bias="0.5" />
Java的:
left.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
enLeft = 1;
enRight = 0;
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
enLeft = 0;
v.performClick();
return false;
} else {
return false;
}
});
当前问题:警告已通过IDE解决,但无法在实际的Android设备上看到此按钮实际上执行的点击操作。
编辑:修复获取点击事件:使用View.setPressed(boolean)
down.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
enFront = 0;
enBack = 1;
left.setPressed(true);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
enBack = 0;
v.performClick();
v.setPressed(false);
return false;
} else {
return false;
}
v.performClick()
。该实现将播放一些声音(如果您在设备上启用了该声音)并调用onClickListener,您可能尚未对其进行覆盖