यदि आप Custom View
स्पष्ट रूप से ओवरराइड का उपयोग नहीं कर रहे हैं onPerformClick
, तो सेको के उत्तर का पालन करने से चेतावनी नहीं निकलेगी।
उनके जवाब के अलावा, कक्षाओं पर समान करने के लिए 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 को कॉल करें, जिसे आपने संभवतः ओवरराइड नहीं किया है