Android在子线程中更新UI的几种方法

Android在子线程中更新UI的几种方法

方式一:Handler和Message

方式二:在子线程中直接调用Activity.runOnUiThread(Runnable action)方法

方式三:在子线程中调用View的post()方法

方式四:在子线程中调用View的postDelayed()方法

方式五:调用Handler的post()方法

// A code block
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final int CHANGE = 1;
    private TextView textView;

    private Button change1;

    private Button change2;

    private Button change3;

    private Button change4;

    private Button change5;

    private Handler handler = new Handler(Looper.getMainLooper()){
        public void handleMessage(Message message){
            switch (message.what){
                case CHANGE:
                    textView.setText("Change on Handler");
                    break;
                default:
                    break;
            }
        }
    };

    private Handler handler2 = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text);
        change1 = findViewById(R.id.change1);
        change2 = findViewById(R.id.change2);
        change3 = findViewById(R.id.change3);
        change4 = findViewById(R.id.change4);
        change5 = findViewById(R.id.change5);

        change1.setOnClickListener(this);
        change2.setOnClickListener(this);
        change3.setOnClickListener(this);
        change4.setOnClickListener(this);
        change5.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.change1:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message message = handler.obtainMessage();
                        message.what = CHANGE;
                        // message.obj = message;
                        handler.sendMessage(message);
                    }
                }).start();
                break;
            case R.id.change2:
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("Change in UiThread");
                    }
                });
                break;
            case R.id.change3:
                textView.post(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("Change in View.post");
                    }
                });
                break;
            case R.id.change4:
                textView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("Change in View.postDelayed");
                    }
                },100);
                break;
            case R.id.change5:
                handler2.post(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("Change in Handler.post");
                    }
                });
                break;
            default:
                break;

        }

    }
}

//
[1]: https://www.jianshu.com/p/72c10f0494f7
[2]: 《第一行代码》第三版

你可能感兴趣的:(android,java)