【问题标题】:Android Ui thread in loopAndroid Ui线程循环
【发布时间】:2013-08-03 19:46:21
【问题描述】:

问题


我有将近 20 个TextView,我需要做一些事情来让它们的背景颜色动态变化。

例如:所有TextView 具有相同的默认背景颜色,然后在 5 秒后第一个将变为红色,而其他人仍然相同,然后在再过 5 秒后第一个 TextView 的背景颜色将变为默认值,第二个 @987654325 @ 的背景颜色会变成红色,并且会这样继续。

我试图将它们放入线程和处理程序的 for 循环中,但它们一起改变了。我该怎么办?

我试过这个代码

Thread color=new Thread() {
    public void run() {
        for(int i=2131230724;i<=2131230742;i++){
            TextView currentText = (TextView) findViewById(i);
            currentText.setBackgroundColor(Color.RED);
            try {
                sleep(2000);
                currentText.setBackgroundColor(Color.TRANSPARENT);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    };

然后我用了

mHandler.post(color);

解决方案


我找到了解决我的问题的方法,如果其他人也需要,请分享。

Runnable myhandler=new Runnable() {
    int id=2131230724;//First textView id
    @Override

    public void run() {

        // TODO Auto-generated method stub
        if(id==2131230724){
            TextView currentText=(TextView)findViewById(R.id.egzersiz01_kelime1);
            currentText.setBackgroundColor(Color.RED);
            id++;
        }
        else if(id==2131230725){
            TextView currentText=(TextView)findViewById(R.id.egzersiz01_kelime2);
            currentText.setBackgroundColor(Color.RED);
            TextView PreText=(TextView)findViewById(R.id.egzersiz01_kelime1);
            PreText.setBackgroundColor(Color.TRANSPARENT);
            id++;
        }//And all other id's to else if as same as mine.


mHandler.postDelayed(myhandler, delay);

在 onCreate() 中

myhandler.run();

我曾尝试使用 for 循环来简化它,但它崩溃了,我不得不将它们全部写出来。 感谢您的帮助...

【问题讨论】:

标签: android loops handler ui-thread dynamic-ui


【解决方案1】:

你可以像这样把参数放到AsynTask,然后

new changeColor().execute(max);


private class changeColor extends AsyncTask<Integer,Integer,Void>{
    int max,cur;
    @Override
    protected void onPreExecute() {
        max=cur=0;
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Integer... number) {
        int max=number[0];
        int cur=1;
        if(max>0)
        while(true ){/*or other condition else*/
            try {
                Thread.sleep(5000);//Sleep 5000s before make change
            } catch (InterruptedException e) {
                Log.i("TAG","InterruptedException");
            }
            publishProgress(cur);
            if(cur==max)
                cur=1;
            else
                cur++;
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
            changeColorTextView(values[0]);
        super.onProgressUpdate(values);
    }
    private void changeColorTextView(int textViewId){
        switch(textViewId){
        case textId1:{
            //do some thing here like:
            //textView2.setBackGroundColor(R.color.RED);
            //textView1.setBackGroundColor(default);
            break;
        }
        //....
        }
    }


}

【讨论】:

  • 感谢您的回答,但我是 Android 开发的新手,我将我的一个文本视图 id 写入了 textId1(在 switch 情况下),但它没有工作或者没有得到错误只是没有任何改变,我应该写什么而不是“new changeColor().execute(max);”中的最大值?
猜你喜欢
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 2015-03-01
相关资源
最近更新 更多