如何在一段时间后取消选择视图而不冻结活动

发布于 2024-10-07 18:31:56 字数 960 浏览 0 评论 0原文

我需要一些关于我的代码的好建议。 这就是我想做的。

我有一个活动,其中有一些可供用户选择的视图。假设用户选择了一个视图,我想在 5 秒后自动取消选择该视图。 我通过线程来完成此操作。

当用户选择视图时,我

Deselector deselect = new Deselector(mp.getDuration(), clickedview);
deselect.start();   

在活动中调用... ...。

取消选择器类:

class Deselector extends Thread
{
int millis=0;
View view = null;

Deselector(int millis, View view)
{
    this.millis = millis;
    this.view = view ; 
}

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        this.sleep(millis);
        view.setSelected(false);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

我的程序崩溃了,logkitty 说

12-11 14:29:37.457: ERROR/AndroidRuntime(3263): android.view.ViewRoot$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸其视图。

怎样做才正确呢?

预先感谢

m.d.

i need some good advice for my code.
here is what i want to do.

i have an activity that has some views that can be selected by user. assumed that the user selected a view, i want to deselect this view automatically after, let's say, 5 seconds.
I do this by a thread.

when the user selects the view, i call...

Deselector deselect = new Deselector(mp.getDuration(), clickedview);
deselect.start();   

...in the activity.

the deselector class:

class Deselector extends Thread
{
int millis=0;
View view = null;

Deselector(int millis, View view)
{
    this.millis = millis;
    this.view = view ; 
}

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        this.sleep(millis);
        view.setSelected(false);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

my program crashes and logkitty says

12-11 14:29:37.457: ERROR/AndroidRuntime(3263): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

how to do it right?

thanks in advance

m.d.

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

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

发布评论

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

评论(2

迷离° 2024-10-14 18:31:56

在小部件或处理程序(而不是后台线程)上使用 postDelayed() 来在您建议的延迟之后执行工作。

Use postDelayed() on a widget or a Handler, rather than a background thread, to do work after your proposed delay.

离旧人 2024-10-14 18:31:56

我通过对我的 deselctor runnable 执行以下操作来使其工作,

clickedview.postDelayed(new Deselector(clickedview), mp.getDuration());

现在要短一些:

class Deselector implements Runnable
{
View view = null;

Deselector(View view)
{
    this.view = view ; 
}

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        view.setSelected(false);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

i got it working by doing the following

clickedview.postDelayed(new Deselector(clickedview), mp.getDuration());

with my deselctor runnable now a bit shorter:

class Deselector implements Runnable
{
View view = null;

Deselector(View view)
{
    this.view = view ; 
}

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        view.setSelected(false);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

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