0

我正在使用 Eclipse IDE 在 android 中显示带有文本视图的数字。我必须在每个数字后延迟 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++;
    }
4

4 回答 4

1

您不能阻止 UI 线程。通常,当您想要延迟做某事时,您应该考虑使用Handler. 在这种情况下,仅使用以下postDelayed功能可能会更简单view

// 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
于 2013-03-10T05:05:43.830 回答
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 );
                }
    }   
于 2013-03-10T05:10:08.980 回答
1

您应该在此处使用 Handler,因为它们不会阻塞 UI 线程。如上所述。使用

posDelayed(your_runnable, deley_time_in_millis)

有关更多信息,请参阅Handler 文档和教程:Android Background Processing with Handlers and AsyncTask and Loaders

于 2013-03-10T05:47:02.933 回答
0

请执行下列操作

Handler handler = new Handler();

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

            public void run() {
                // peform second action
            }
        }, 2*60*1000);
于 2013-03-10T05:08:29.800 回答