开发者

Android chronometer ticks once and stops

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. 开发者_Python百科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);


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.


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);
    }
  }

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜