本文介绍了安卓:更新UI的一个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些简单的code,但它似乎没有工作..任何建议吗?

So I have some simple code but it seems to not be working.. any suggestions?

我只是想一个图像显示在一个按钮是pssed在2秒钟后成为无形$ P $。

I just want an image to show after a button is pressed then become invisible after 2 seconds.

button.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
      firstImage.setVisibility(ImageView.VISIBLE);
      // delay of some sort
      firstImage.setVisibility(ImageView.INVISIBLE);
   }
}

图片从未表明,它始终保持无形的,我应该以另一种方式来实现这一点?我试过处理..但它没有工作,除非我没有错。

The image never shows, it always stays invisible, should I be implementing this in another way? I've tried handlers.. but it didn't work, unless I did it wrong.

推荐答案

从不让你的UI线程睡眠!

Never make your UI thread sleep!

做到这一点:

final Handler handler = new Handler();

button.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
      firstImage.setVisibility(ImageView.VISIBLE);
      handler.postDelayed(new Runnable(){
            public void run(){
                 firstImage.setVisibility(ImageView.INVISIBLE);
            }
      }, DELAY);
   }
}

在这里您将延迟设置为2000(毫秒)。

Where you would set DELAY as 2000 (ms).

这篇关于安卓:更新UI的一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:06