简体   繁体   English

我应该为我的Android应用程序使用什么EVENT LISTENER

[英]what EVENT LISTENER should i use for my android app

In my app, when one particular image button is clicked and held, i must be able to calculate the time for which the image button was held pressed. 在我的应用程序中,当单击并按住一个特定图像按钮时,我必须能够计算按下图像按钮的时间。 Can any one help me by giving some simple guidance or sample code. 任何人都可以通过提供一些简单的指导或示例代码来帮助我。 i am really stuck up here. 我真的被困在这里了。 Is there any specific event listener for this particular requirement. 是否有针对此特定要求的特定事件侦听器。 I am writing this app specifically for a touch screen phones only. 我正在专门为触摸屏手机编写这个应用程序。

What you want to use is this: 你想用的是:

OnTouchListener touchListener = new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        Log.d(LOG_TAG, "Action is: " + action);

        switch (action){
        case MotionEvent.ACTION_DOWN:
            timeAtDown = SystemClock.elapsedRealtime();
            break;

        case MotionEvent.ACTION_UP:
            long durationOfTouch = SystemClock.elapsedRealtime() - timeAtDown;
            Log.d(LOG_TAG, "Touch event lasted " + durationOfTouch + " milliseconds.");
            break;
        }
        return false;
    }

};

button.setOnTouchListener(touchListener);

timeAtDown is a long defined as a class field, since it needs to persist between calls to the touchListener. timeAtDown是一个long定义为类字段,因为它需要在对touchListener的调用之间保持不变。 Using this you don't interfere with the button's normal operation either; 使用此功能,您也不会干扰按钮的正常操作; you can set a click listener which will function properly. 你可以设置一个能正常运行的点击监听器。 Note: The 'click' operation doesn't happen until the touch event's action goes from DOWN (or MOVE) to UP. 注意:直到触摸事件的动作从DOWN(或MOVE)变为UP时才会发生'click'操作。

Experiment with the on*** callbacks from http://developer.android.com/reference/android/view/KeyEvent.Callback.html . 尝试使用http://developer.android.com/reference/android/view/KeyEvent.Callback.html上的on ***回调。

For example Button.onKeyDown, save the current time in a variable and Button.onKeyDown calculate the difference from last saved time. 例如Button.onKeyDown,将当前时间保存在变量中,Button.onKeyDown计算与上次保存时间的差值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该在android中使用哪种类型的事件监听器? - What type of event listener should i use in android? 我应该在Android应用程序上使用哪种数据存储方式? - What kind of data storage should i use on my android app? 我的Android Chromecast应用程序游戏应使用哪个ControlCategory? - What ControlCategory should I use for my android Chromecast app game? 我应该使用什么在我的android应用上显示流式URL? - What should i use to display a streaming url on my android app? 我应该为我的android应用使用哪种图像 - What kind of images should i use for my android app 我应该在Android开发中使用侦听器接口或处理程序来进行事件回调吗? - Should I use a listener interface or handler for event callbacks in Android development? 我应该使用什么UUID(bluetooth LE)在我的android应用程序和lightblue应用程序之间进行连接? - What UUID(bluetooth LE) should I use to connect between my android app and lightblue app? 我应该在Android中使用什么布局类型才能让我的应用适合所有屏幕尺寸? - What layout type should i use in android in order for my app to fit all screen sizes? 我的 Android 应用程序和 ASP.NET Web 服务之间应该使用什么解密? - What decryption should i use between my Android app and ASP.NET Webservice? 我的 android 应用程序在 8 个活动后崩溃,我该怎么办? - my android app crashes after 8 activities,what should i do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM