Android 计时器滴答一次后停止

发布于 2024-09-28 17:55:50 字数 1579 浏览 4 评论 0 原文

我需要在 10 秒后隐藏 TextView。我想我可以实现一个计时器,当经过的时间大于 10000 毫秒时,我隐藏 TextView。我的问题是计时器只滴答一次然后就停止了。知道我错过了什么吗?

Activity ctx = this;
...

private void ShowText(String message)
    {
        txtProceed.setText(message);
        txtProceed.setVisibility(View.VISIBLE);


        chronoHideText = new Chronometer(ctx);
        chronoHideText.setOnChronometerTickListener(new OnChronometerTickListener()
        {
               public void onChronometerTick(Chronometer arg0) {

                  long elapsed = SystemClock.elapsedRealtime() - chronoHideText.getBase();
                  Log.i("Chrono",String.valueOf(elapsed));
                   if (elapsedTime>10000)
                   {
                       txtProceed.setVisibility(View.INVISIBLE);
                       chronoHideText.stop();
                   }
               }
        }
        );

        chronoHideText.setBase(SystemClock.elapsedRealtime());
        chronoHideText.start();

    }

谢谢贾努斯的帮助。现在效果很好的解决方案是:

Handler splashHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                    switch (msg.what) {
                    case 0:
                            //remove SplashScreen from view
                            txtProceed.setVisibility(View.INVISIBLE);
                            break;
                    }
                    super.handleMessage(msg);
            }
    };

    Message msg = new Message();
    msg.what = 0;
    splashHandler.sendMessageDelayed(msg, 10000);

I need to hide a TextView after 10 seconds. I thought I could implement a Chronometer and when elapsed time is bigger than 10000 millis I hide the TextView. My problem is that the chronometer only ticks once and then stops. Any idea what I've missed ?

Activity ctx = this;
...

private void ShowText(String message)
    {
        txtProceed.setText(message);
        txtProceed.setVisibility(View.VISIBLE);


        chronoHideText = new Chronometer(ctx);
        chronoHideText.setOnChronometerTickListener(new OnChronometerTickListener()
        {
               public void onChronometerTick(Chronometer arg0) {

                  long elapsed = SystemClock.elapsedRealtime() - chronoHideText.getBase();
                  Log.i("Chrono",String.valueOf(elapsed));
                   if (elapsedTime>10000)
                   {
                       txtProceed.setVisibility(View.INVISIBLE);
                       chronoHideText.stop();
                   }
               }
        }
        );

        chronoHideText.setBase(SystemClock.elapsedRealtime());
        chronoHideText.start();

    }

Thank you Janusz for your help. The solution that works great now is:

Handler splashHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                    switch (msg.what) {
                    case 0:
                            //remove SplashScreen from view
                            txtProceed.setVisibility(View.INVISIBLE);
                            break;
                    }
                    super.handleMessage(msg);
            }
    };

    Message msg = new Message();
    msg.what = 0;
    splashHandler.sendMessageDelayed(msg, 10000);

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

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

发布评论

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

评论(2

海风掠过北极光 2024-10-05 17:55:51

您可以使用 handleMessage 方法。

这样你就不需要自己测量时间并每秒或毫秒进行计算工作。在 Android 操作系统传送消息之前,您的应用程序不会占用任何 cpu 时间。

You can use a handler that sends a delayed message.
The method sendMessageDelayed gives you the ability to specify a time and after that time elapsed you will get a message. If you only need to do one thing after the elapsed time you can just hide the view in your handleMessage method.

This way you don't need to measure the time yourself and do computation work every second or millisecond. Your app won't take any cpu time until the message is delivered by the Android OS.

半世晨晓 2024-10-05 17:55:51
public class ButtonVisibility extends Activity {

Button mButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_button);

    mButton = (Button)findViewById(R.id.button);

    new MyTask().execute(null);
}

public class MyTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(10000);
        }
        catch(InterruptedException e) {
            Log.e("MyTask", "InterruptedException", e);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        mButton.setVisibility(View.INVISIBLE);
    }
  }

}

public class ButtonVisibility extends Activity {

Button mButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_button);

    mButton = (Button)findViewById(R.id.button);

    new MyTask().execute(null);
}

public class MyTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(10000);
        }
        catch(InterruptedException e) {
            Log.e("MyTask", "InterruptedException", e);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        mButton.setVisibility(View.INVISIBLE);
    }
  }

}

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