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" />
자바 :
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를 호출합니다. 아마도 재정의하지 않았을 것입니다