TextView每秒通过睡眠线程更新内容

时间:2014-08-22 17:07:17

标签: android multithreading textview

在这个应用程序中,TextView的内容应该每秒用睡眠线程更改/更新。 单击按钮时整个过程开始。

这里的Firstable是没有线程的普通代码:

public class MainActivity extends ActionBarActivity {

    Button btn;
    TextView tw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        tw = (TextView) findViewById(R.id.tw);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                tw.setText("1");    //This is the TextView Content, it should update every second with a sleep thread
                tw.setText("2");
                tw.setText("3");





            }
        });
    }
}

这是添加(不工作)睡眠线程的代码:

public class MainActivity extends ActionBarActivity {

    Button btn;
    TextView tw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        tw = (TextView) findViewById(R.id.tw);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                tw.setText("1");       


                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }



                tw.setText("2");


                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }



                tw.setText("3");





            }
        });
    }
}

谢谢

1 个答案:

答案 0 :(得分:1)

问题是你阻止了主UI(sleep)线程,从而给你意想不到的结果。

如果你想每秒更新一次,你需要使用处理程序

<强>样品:

public class MainActivity extends ActionBarActivity {

Button btn;
TextView tw;
int incre = 1; 
Handler handler;
Runnable run;

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.btn);
    tw = (TextView) findViewById(R.id.tw);


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tw.setText(incre++ + "");
            handler = new Handler();
            run = new Runnable() {

                @Override
                public void run() {
                    tw.setText(incre++ + ""); //set the textview text HERE every 1 second
                    if(incre != 3) //checks if it is not already 3 second
                         handler.postDelayed(run, 1000); //run the method again
                    else
                       incre -= 2;
                }
            };
            handler.postDelayed(run, 1000); //will call the runnable every 1 second


        }
    });
}

}