Android OnTouchEvent, onClick, onLongClick调用机制

本文探讨了在Android开发中,当View同时设置onClick、onLongClick和onTouch事件时的执行顺序。根据官方文档,onLongClick返回true表示事件已被处理,而onTouch在接收到down动作并返回false时,将不处理后续动作。通过测试程序展示了不同情况下,这三个事件的触发情况:1) onTouch返回false且onLongClick返回false时,长按会依次调用onTouch、onLongClick和onClick;2) onTouch返回false且onLongClick返回true时,仅调用onTouch和onLongClick;3) onTouch返回true时,仅调用onTouch。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Android开发中,我们经常会对一个View设置onClick,onLongClick,onTouch事件,有时还会同时设置这三个事件,那么在同时设置这三个时候,执行顺序是什么样呢?

首先,官方文档上面对onLongClick()和onTouch()的描述如下:

onLongClick() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners.

大致意思就是:onLongClick()方法会返回一个boolean值,如果是true,表示已经处理了此事件,不需要继续传递;如果是false,表示没有处理,事件会继续到其他的监听事件。

onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.

大致意思是:onTouch()方法,会包含多个行为,ACITON_DOWN,ACTION_MOVE,ACTION_UP。如果返回值时true,就表示会处理这个事件,就不会再调用onClick或者onLongClick,如果返回false,则会继续调用onLongClick。

写个简单的测试程序:

package com.qwei.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class MainActivity extends Activity 
{
    private final static String TAG = MainActivity.class.getName();
	private TextView clickMe,showLog;
    private String log;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        clickMe = (TextView) findViewById(R.id.click_me);
        showLog = (TextView) findViewById(R.id.show_log);

        clickMe.setOnLongClickListener(new OnLongClickListener() {  

			@Override
			public boolean onLongClick(View arg0) {
				// TODO Auto-generated method stub
				 Log.d("TAG", "onLongClick is called.............");  
	                log = log +"onLongClick is called.............\n";
	                showLog.setText(log);
				return false;
			}  
        }); 
        clickMe.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {   
                Log.d("TAG", "onClick is called.............");  
                log = log +"onClick is called.............\n";
                showLog.setText(log);
            }  
        }); 

        clickMe.setOnTouchListener(new OnTouchListener() {  
              
            @Override  
            public boolean onTouch(View arg0, MotionEvent arg1) {  
                //Log.d("TAG", "onTouch is called.............");  
                switch (arg1.getAction()) {  
                case MotionEvent.ACTION_DOWN:  
                    Log.d("TAG", "ACTION_DOWN.............");  
                    log = log +"oACTION_DOWN............\n";
                    showLog.setText(log);
                    break;  
                case MotionEvent.ACTION_MOVE:  
                    Log.d("TAG", "ACTION_MOVE.............");  
                    log = log +"ACTION_MOVE.............\n";
                    showLog.setText(log);
                    break;  
                case MotionEvent.ACTION_CANCEL:  
                    Log.d("TAG", "ACTION_CANCEL.............");  
                    log = log +"ACTION_CANCEL............\n";
                    showLog.setText(log);
                    break;  
                case MotionEvent.ACTION_UP:  
                    Log.d("TAG", "ACTION_UP.............");  
                    log = log +"ACTION_UP............\n";
                    showLog.setText(log);
                    break;  
                default:  
                    break;  
                }  
                return false;  
            }  
        });  
    }
}

当onTouch返回false,onLongClick也返回false,长按view时,会调用onTouch,一段时间之后调用onLongClick,抬起手指的时候调用onClick,如下图所示:


当onTouch返回false,onLongClick也返回true,长按view时,会调用onTouch,一段时间之后调用onLongClick,抬起手指的时候不会调用onClick,如下图:




当onTouch返回true,长按view时,会调用onTouch,onLongClick和onClick都不会被调用,如下图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值