1.android onTouchEventåsetOnTouchListenerä¸onTouchçåºå«
2.Android onTouchEventåsetOnTouchListenerä¸onTouchçåºå«
android onTouchEventåsetOnTouchListenerä¸onTouchçåºå«
androidä¸onTouchEventåsetOnTouchListenerä¸onTouchçåºå«å¯ä»¥æäºäººå¹¶ä¸äºè§£ï¼å ¶å®è¦è¯´æç½è¿ä¸ªé®é¢å è¦è¯´ä¸androidçäºä»¶ä¼ éæºå¶ãé¦å çä¸ä»¥ä¸ç代ç 1ï¼è¿æ®µä»£ç ååºäºViewå¤çäºä»¶çè¿ç¨ï¼ä»£ç 1ï¼publicbooleandispatchTouchEvent(MotionEventevent){ if(mOnTouchListener!=null&&mOnTouchListener.onTouch(this,源码福利app源码event)){ returntrue;}returnonTouchEvent(event);}å¨ä»£ç 1ä¸æä¸ç¹é¦å éè¦æç¡®ï¼é£å°±æ¯å¦ædispatchTouchEventè¿åå¼ä¸ºtrueåæ¬æ¬¡äºä»¶è¢«ç³»ç»æ¶èæï¼å°±æ¯è¢«æ§ä»¶å¤çäºï¼ï¼ç¶åä¸ä¸ªæ°çäºä»¶ä¼è¢«ä¼ å ¥ï¼å¦ædispatchTouchEventè¿åå¼ä¸ºfalseï¼åä¸ä¼ææ°çäºä»¶è¢«ä¼ å ¥ã好ï¼æç½äºè¿ä¸ç¹åçé£ä¸ªä»£ç 1ä¸ifè¯å¥åé¢çæ¡ä»¶mOnTouchListener!=null&&mOnTouchListener.onTouch(this,event)ï¼mOnTouchListenerå¯¹è±¡å ¶å®ä¸å°±æ¯ä½ åççå¬å¨å¯¹è±¡åï¼æ¯å¦ä¸é¢ä»£ç 2çè¿ä¸ªå¿å对象ã代ç 2ï¼xxxView.setOnTouchListener(newOnTouchListener(){ @OverridepublicbooleanonTouch(Viewv,MotionEventevent){ returntrue;}});好ï¼æ们åçä¸ä»£ç 2ä¸onTouchäºä»¶ä¸çè¿åå¼ï¼å¦æ代ç 2returntrueå代表ä»ä¹å¢ï¼ä»£è¡¨ä»£ç 1ä¸å°ä¸ä¼æ§è¡returnonTouchEvent(event);è¯å¥ï¼è¿ä¹å°±æ¯è¯´è¿æ¬¡ç³»ç»äºä»¶ä¼è¢«æ¶èæï¼å°ä¼å次æ§è¡dispatchTouchEventè¿ä¸ªæ¹æ³ãå¦æ代ç 2returnfalseå代表代ç 1ä¼æ§è¡onTouchEvent(event);è¿ä¸ªæ¹æ³ï¼æ¬æ¬¡äºä»¶æ¯å¦ä¼è¢«æ¶èæå°åå³äºonTouchEventçè¿åå¼ãå个å°æ»ç»å ¶å®å°±æ¯å¦æsetOnTouchListenerä¸çonTouchæ¹æ³è¿åå¼æ¯trueåonTouchEventæ¹æ³å°ä¸ä¼è¢«æ§è¡ï¼åªæå½setOnTouchListenerä¸çonTouchæ¹æ³è¿åå¼æ¯falseæ¶onTouchEventæ¹æ³æ被æ§è¡ã以ä¸è¯´çæ åµéç¨äºView对象èä¸æ¯ViewGroup对象ï¼ViewGroup对象ä¸æ¬¡ååæã
Android onTouchEventåsetOnTouchListenerä¸onTouchçåºå«
å¦æä½ ä¸å¨æäºä»¶çä¼ éï¼é£å®é æææ¯å®å ¨ä¸æ ·çï¼åªæ¯å®ç°æ¹å¼ä¸ä¸æ ·ãå¦ä¸ä»£ç ï¼public class TestActivity extends Activity implements View.OnTouchListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
Button button = findViewById(R.id.my_button);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//â¦â¦ä½ ç代ç
return false;
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.my_button){
//â¦â¦ä½ ç代ç
}
return false;
}
}
ä¸ä¸ªæ¯éè¿setOnTouchListeneræ¹æ³å®ç°çï¼ä¸ä¸ªæ¯éè¿implements View.OnTouchListenerå®ç°çãæç»é½æ¯ååºR.id.my_buttonè¿ä¸ªæé®çäºä»¶ã