对话框中的 onTouchEvent

发布于 2024-12-14 04:07:12 字数 1334 浏览 3 评论 0原文

我已经在 Android Dialog 中实现了按钮的 onLongClickListener ,它使用计时器自动减少数字。这是代码

myIncreaseTimer = new Timer();
final Runnable runnable_decrease = new Runnable() {
    public void run() {
        int txt = Integer.parseInt(length.getText().toString());
        txt -= 1;
        length.setText(String.valueOf(txt));
        if (txt == 0) {
            myIncreaseTimer.cancel();
            myIncreaseTimer.purge();
            myIncreaseTimer = new Timer();
        }
    }
};
btnMinus.setOnLongClickListener(new View.OnLongClickListener() {
    public boolean onLongClick(View v) {
        myIncreaseTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(runnable_decrease);
            }
        }, 0, 300);
        return true;
    }
});

当我在活动而不是对话框中时,当用户触摸屏幕时,我可以通过 onTouchEvent 停止减少

@Override 
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        myIncreaseTimer.cancel();
        myIncreaseTimer.purge();
        myIncreaseTimer = new Timer();
    }
    return true;
}

但是,我不能在对话框中执行此操作。更重要的是,对话框没有onTouchEvent。

有谁知道我如何在Dialog中实现onTouchEvent或者我应该在Dialog中停止增加计时器?

I have implemented button's onLongClickListener in Android Dialog which auto-decreases number using timers. Here is the code

myIncreaseTimer = new Timer();
final Runnable runnable_decrease = new Runnable() {
    public void run() {
        int txt = Integer.parseInt(length.getText().toString());
        txt -= 1;
        length.setText(String.valueOf(txt));
        if (txt == 0) {
            myIncreaseTimer.cancel();
            myIncreaseTimer.purge();
            myIncreaseTimer = new Timer();
        }
    }
};
btnMinus.setOnLongClickListener(new View.OnLongClickListener() {
    public boolean onLongClick(View v) {
        myIncreaseTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(runnable_decrease);
            }
        }, 0, 300);
        return true;
    }
});

When I am inside of activity, not dialog, I can stop the decrease via onTouchEvent when a user touches the screen

@Override 
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        myIncreaseTimer.cancel();
        myIncreaseTimer.purge();
        myIncreaseTimer = new Timer();
    }
    return true;
}

However, I cannot do this in the dialog. Even more, the dialog does not have onTouchEvent.

Does anyone know how can I either implement onTouchEvent in Dialog or where should I stop the increase timer in the Dialog?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独孤求败 2024-12-21 04:07:12

您无法处理 Dialog 上的 onTouch,因此

创建一个 Activity,在其上实现 onTouchListener,
在清单文件中

<activity android:theme="@android:style/Theme.Dialog">

所以,现在你的活动看起来像一个对话框,

You can't handle onTouch on Dialog so

Create a Activity, implement onTouchListener on it,
and in manifest file

<activity android:theme="@android:style/Theme.Dialog">

So, now your activity is look like a DialogBox,

轮廓§ 2024-12-21 04:07:12

因为 Dialog 并非设计用于处理原始 onTouchEvents。考虑创建一个看起来像对话框的活动(透明主题,没有标题栏)并覆盖它的 onTouchEvent 方法。

because Dialog is not designed to handle raw onTouchEvents. consider creating an activity that would look like a dialog (transparent theme, no title bar) and override it's onTouchEvent method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文