0

这是我的 onCreate 方法

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // Show the splash screen
        setContentView(R.layout.progressbar);
        initActionbar();




        mytext=(TextView)findViewById(R.id.progresstextview1);
        mytext1 = (TextView)findViewById(R.id.progresstextview2);
        mytext2 = (TextView)findViewById(R.id.progresstextview3);



        Thread t = new Thread();
        Thread t1 = new Thread();
        Thread t2 = new Thread();

        t.start();
        mytext.setVisibility(View.VISIBLE);


        t1.start();
        mytext1.setVisibility(View.VISIBLE);


        t2.start();
        mytext2.setVisibility(View.VISIBLE);
  }

这是我的运行方法

   @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<1000;i++)
        {

        }
    }

我希望我的 3 TextView 在延迟的情况下一个接一个地加载。问题是所有三个 textview 都首先加载并且延迟没有发生。另一个问题是主 UI 线程在几秒钟后启动。任何帮助这方面将不胜感激!

4

1 回答 1

1

做这些事情的好方法是使用 android.os.Handler

参见示例:

        mytext=(TextView)findViewById(R.id.progresstextview1);
        mytext1 = (TextView)findViewById(R.id.progresstextview2);
        mytext2 = (TextView)findViewById(R.id.progresstextview3);

        uiThreadHandler  = new Handler();    
        showDelayed(mytext, 1000);
        showDelayed(mytex1, 2000);
        showDelayed(mytex2, 3000);
  }

  public void showDelayed(final View v, int delay){
      uiThreadHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                v.setVisibility(View.Visible);
            }
        }, delay);
  }

另外,请记住:线程创建可能是一项昂贵的操作,所以尽量避免这样的代码行

new Thread().start();

相反 - 尝试使用另一种方法,或者至少使用 Executor 框架中的 ThreadPool

于 2013-09-23T09:51:19.017 回答