ในกรณีที่คุณไม่ได้ใช้สิ่ง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 ซึ่งคุณอาจไม่ได้ลบล้าง