目標:我有一個已經包含的功能onTouch,我想保留,但在上添加功能的自定義按鈕。Android:添加setOnTouchListener而不覆蓋現有的?
問題:像這樣添加onTouch將覆蓋原始的自定義按鈕功能,從而不再執行原始的所需行爲。具有註釋「@Override」與否並不能改變什麼:
myButton.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
MainActivity.super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//Extra behavior needed
return true;
}
return false;
}
});
自定義按鈕本身:
public class CustomButton extends Button implements View.OnTouchListener {
...
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
//Original needed behavior. Need to keep.
break;
...
}
return false;
}
...
}
任何解決方案可以考慮?謝謝!
@Override將只檢查方法是否可用於超類 –