2013-03-10 35 views
0

我在android中使用eclipse IDE在文本视图中显示数字。每个号码后我必须执行2分钟的延迟。我怎样才能做到这一点? 这里是我的代码:我该如何实现显示2个数字之间的延迟

int counter1 = 0; 
    String stringVal; 
    TextView textview1; 

    textview1 = (TextView) findViewById(R.id.textView1); 

    while (counter1 < 10) { 
     stringVal = Integer.toString(counter1); 
     textview1.setText(stringVal); 
     counter1++; 
    } 

回答

1

你不能阻塞UI线程。通常当你想延迟做某事时,你应该考虑使用Handler。在这种情况下,它可能是简单的只是使用的viewpostDelayed功能:

// needs to be `final` so it can be referenced from inside the Runnable 
final TextView textview1 = (TextView) findViewById(R.id.textView1); 
final Runnable r = new Runnable() { 
    int counter = 0; 
    public void run() { 
     textview1.setText(Integer.toString(counter++)); 
     if (counter < 10) { 
      textview1.postDelayed(this, 1000 * 60 * 2); // 2 minutes 
     } 
    } 
} 

r.run(); // starts things off 
+0

谢谢泰德霍普。这工作正常。 – Mano2733 2013-03-10 16:48:30

+0

@ Mano2733 - 如果它解决了您的问题,您可以考虑接受答案。要接受答案,请点击答案左侧的复选标记,将其标记为解决方案。让别人知道什么工作。它也给我们两个小小的声望提升。 :) – 2013-03-10 16:54:52

+0

我已经接受它。 – Mano2733 2013-03-10 16:59:41

0

做以下

Handler handler = new Handler(); 

// perform first action 
handler..postDelayed(new Runnable() { 

      public void run() { 
       // peform second action 
      } 
     }, 2*60*1000); 
+0

谢谢StinePike。 – Mano2733 2013-03-10 16:56:00

+0

欢迎您:) – stinepike 2013-03-11 02:05:19

1

可以使用的AsyncTask,并在每onProgressUpdate设置新的TextView:

protected class InitTask extends AsyncTask<Context, Integer, String> 
    { 
     // -- run intensive processes here 
     // -- notice that the datatype of the first param in the class definition matches the param passed to this method 
     // -- and that the datatype of the last param in the class definition matches the return type of this method 
       @Override 
       protected String doInBackground(Context... params) 
       { 
         //-- on every iteration 
         //-- runs a while loop that causes the thread to sleep for 2 minutes 
         //-- publishes the progress - calls the onProgressUpdate handler defined below 
         //-- and increments the counter variable i by one 
         int i = 0; 
         while(i <= 50) 
         { 
           try{ 
             Thread.sleep(2*60*1000); 
             publishProgress(i); 
             i++; 
           } catch(Exception e){ 
             Log.i("makemachine", e.getMessage()); 
           } 
         } 
         return "COMPLETE!"; 
       } 

       // -- gets called just before thread begins 
       @Override 
       protected void onPreExecute() 
       { 
         Log.i("makemachine", "onPreExecute()"); 
         super.onPreExecute(); 

       } 

       // -- called from the publish progress 
       // -- notice that the datatype of the second param gets passed to this method 
       @Override 
       protected void onProgressUpdate(Integer... values) 
       { 
         super.onProgressUpdate(values); 
         Log.i("makemachine", "onProgressUpdate(): " + String.valueOf(values[0])); 
         _percentField.setText((values[0] * 2) + "%"); 
         _percentField.setTextSize(values[0]); 
       } 

       // -- called if the cancel button is pressed 
       @Override 
       protected void onCancelled() 
       { 
         super.onCancelled(); 
         Log.i("makemachine", "onCancelled()"); 
         _percentField.setText("Cancelled!"); 
         _percentField.setTextColor(0xFFFF0000); 
       } 

       // -- called as soon as doInBackground method completes 
       // -- notice that the third param gets passed to this method 
       @Override 
       protected void onPostExecute(String result) 
       { 
         super.onPostExecute(result); 
         Log.i("makemachine", "onPostExecute(): " + result); 
         _percentField.setText(result); 
         _percentField.setTextColor(0xFF69adea); 
         _cancelButton.setVisibility(View.INVISIBLE); 
       } 
    } 
+0

谢谢AVEbrahimi。这是复杂的解决方案,我是初学者。 – Mano2733 2013-03-10 16:55:17