设置 Android 按钮在一段时间后可见?

发布于 2024-12-07 19:41:07 字数 713 浏览 0 评论 0原文

我有一个按钮,我不想在运行一定时间(例如 5 秒?)之前单击该按钮,我尝试创建一个像这样的线程

    continueButtonThread =  new Thread()
    {
        @Override
        public void run()
        {
            try {
                synchronized(this){
                    wait(5000);
                }
            }
            catch(InterruptedException ex){                    
            }

            continueButton.setVisibility(0);                
        }
    };

    continueButtonThread.start();

但是我无法在不同的线程中修改按钮的 setVisibility 属性线。这是来自 LogCat 的错误:

10-02 14:35:05.908: ERROR/AndroidRuntime(14400): android.view.ViewRoot$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸其视图。

还有其他方法可以解决这个问题吗?

I have a button that I don't want to be clickable until a certain amount of time has run (say, 5 seconds?) I tried creating a thread like this

    continueButtonThread =  new Thread()
    {
        @Override
        public void run()
        {
            try {
                synchronized(this){
                    wait(5000);
                }
            }
            catch(InterruptedException ex){                    
            }

            continueButton.setVisibility(0);                
        }
    };

    continueButtonThread.start();

But I can't modify the setVisibility property of the button within a different thread. This is the error from the LogCat:

10-02 14:35:05.908: ERROR/AndroidRuntime(14400): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Any other way to get around this?

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

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

发布评论

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

评论(4

つ可否回来 2024-12-14 19:41:07

问题是您只能在 UI 线程中触摸 Activity 的视图。您可以使用 runOnUiThread 函数来完成此操作。我想建议你使用

handler.postDelayed(runnable, 5000)`

The problem is that you can touch views of your activity only in UI thread. you can do it by using runOnUiThread function. I would like to suggest you to use

handler.postDelayed(runnable, 5000)`
九公里浅绿 2024-12-14 19:41:07

您必须从 UI 线程更新您的视图。您正在做的是从非 ui 线程进行更新。

使用或使用处理程序并发出信号 hand.sendMessage(msg)

contextrunOnUiThread(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub

        }});

当您认为更新视图可见性的正确时机时,

 Handler hand = new Handler()        
        {

            @Override
            public void handleMessage(Message msg) {
                /// here change the visibility
                super.handleMessage(msg);
            }

        };

You must update your view from UI-thread. What you are doing is you are updating from non-ui-thread.

Use

contextrunOnUiThread(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub

        }});

or use handler and signalize hand.sendMessage(msg) when you think is right time to update the view visibility

 Handler hand = new Handler()        
        {

            @Override
            public void handleMessage(Message msg) {
                /// here change the visibility
                super.handleMessage(msg);
            }

        };
各空 2024-12-14 19:41:07

You can use the postDelayed method from the View class (A Button is a child of View)

一梦浮鱼 2024-12-14 19:41:07

这是我找到的简单答案

Button button = (Button)findViewBYId(R.id.button);
button .setVisibility(View.INVISIBLE);
button .postDelayed(new Runnable() {
    public void run() {
        button .setVisibility(View.VISIBLE);
    }
}, 7000);

here is simple answer i found

Button button = (Button)findViewBYId(R.id.button);
button .setVisibility(View.INVISIBLE);
button .postDelayed(new Runnable() {
    public void run() {
        button .setVisibility(View.VISIBLE);
    }
}, 7000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文