Android数据绑定view.onTouchListener [英] Android data binding view.onTouchListener

查看:236
本文介绍了Android数据绑定view.onTouchListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android在数据绑定中存在

<Button android:onClick="@{handler.someButtonClick()}"/>

,在其Handler class中,其侦听器将类似于:

and in its Handler class its listener will be some how like:

public View.OnClickListener someButtonClick() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        };
    }

我想为Button实现OnTouchListener,我可以知道该按钮何时被按下以及何时被释放

I want to implement OnTouchListener for a Button that I could know that when the button is pressed and when it is released

赞:

// Check if the button is PRESSED
if (event.getAction() == MotionEvent.ACTION_DOWN){
     //do some thing          
}// Check if the button is RELEASED
else if (event.getAction() == MotionEvent.ACTION_UP) {
    //do some thing                     
}

是否有任何可能的方法来完成此任务.

Is there any possible way to accomplish this task.

推荐答案

这是您可以用来解决此问题的解决方法.

Here is a workaround which you can use to do this.

@BindingAdapter("touchListener")
public void setTouchListener(View self,boolean value){
    self.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            // Check if the button is PRESSED
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                //do some thing
            }// Check if the button is RELEASED
            else if (event.getAction() == MotionEvent.ACTION_UP) {
                //do some thing
            }
            return false;
        }
    });
}

然后输入xml

<Button  app:touchListener="@{true}"/>

这篇关于Android数据绑定view.onTouchListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆