1

很抱歉提出这样一个基本问题,实际上我需要在特定时间间隔后调用一个方法,该方法实际上是在 android 中为 textView 分配一个文本,这应该会改变。所以请建议我最好的方法来做到这一点。感谢你在期待。

{
         int splashTime=3000;
         int waited = 0;
         while(waited < splashTime)
         {
             try {
                  ds.open();
                  String quotes=ds.getRandomQuote();
                  textView.setText(quotes);
                  ds.close();


              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
         }
         waited+=100;
     }
4

3 回答 3

4

你考虑过CountDownTimer吗?例如这样的:

     /**
     * Anonymous inner class for CountdownTimer
     */
    new CountDownTimer(3000, 1000) { // Convenient timing object that can do certain actions on each tick

        /**
         * Handler of each tick.
         * @param millisUntilFinished - millisecs until the end
         */
        @Override
        public void onTick(long millisUntilFinished) {
            // Currently not needed
        }

        /**
         * Listener for CountDownTimer when done.
         */
        @Override
        public void onFinish() {
             ds.open();
              String quotes=ds.getRandomQuote();
              textView.setText(quotes);
              ds.close(); 
        }
    }.start();

当然,你可以把它放在一个循环中。

于 2012-12-14T12:34:41.163 回答
1

您可以使用 Timer 延迟更新您的 UI,如下所示:

    long delayInMillis = 3000; // 3s
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            // you need to update UI on UIThread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ds.open();
                    String quotes=ds.getRandomQuote();
                    textView.setText(quotes);
                    ds.close();
                }
            });
        }
    }, delayInMillis);
于 2012-12-14T12:36:44.200 回答
1

使用处理程序并将其放入 Runnable:

int splashTime = 3000;
Handler handler = new Handler(activity.getMainLooper());
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        try {
            ds.open();
            String quotes=ds.getRandomQuote();
            textView.setText(quotes);
            ds.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}, splashTime);
于 2012-12-14T12:38:51.857 回答