Android学习笔记之:android更新ui的几种常用方法

Android主线程不能执行耗时操作,我们一般是在子线程中执行耗时操作,
 我们在执行完耗时操作后,我们一般可以通过以下几种方式来实现ui界面的更新。
    首先是布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/mTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="16sp" />
    <Button
        android:id="@+id/update_mButton_01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Hander Post"
        android:textSize="15sp" />
    <Button
        android:id="@+id/update_mButton_02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Hander SendMessage"
        android:textSize="15sp" />
    <Button
        android:id="@+id/update_mButton_03"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="RunOnUiThread"
        android:textSize="15sp" />
    <Button
        android:id="@+id/update_mButton_04"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="View Post"
        android:textSize="15sp" />
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

————–代码实现,有注释————————————-

public class MainActivity extends Activity implements OnClickListener {
    private TextView mTextView;
    private Button update_mButton_01;
    private Button update_mButton_02;
    private Button update_mButton_03;
    private Button update_mButton_04;
    private Handler mPostHander = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 1) {
                // 更新UI
                mTextView.setText("通过Hander Send Message更新Ui");
            }
        };
    };

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

    /**
    * 初始化控件 
    * @description:
    * @date 2015-10-8 上午10:55:49
    */
    private void initViews() {
        this.mTextView = (TextView) findViewById(R.id.mTextView);
        this.update_mButton_01 = (Button) findViewById(R.id.update_mButton_01);
        this.update_mButton_02 = (Button) findViewById(R.id.update_mButton_02);
        this.update_mButton_03 = (Button) findViewById(R.id.update_mButton_03);
        this.update_mButton_04 = (Button) findViewById(R.id.update_mButton_04);
    }

    /**
    * 事件监听
    * @description:
    * @date 2015-10-8 上午10:56:02
    */
    private void initEvents() {
        this.update_mButton_01.setOnClickListener(this);
        this.update_mButton_02.setOnClickListener(this);
        this.update_mButton_03.setOnClickListener(this);
        this.update_mButton_04.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.update_mButton_01:// 第一种方式,通过Hander.post方法来实现UI更新
                new Thread() {
                    public void run() {
                        try {
                            sleep(2000);// 休眠2秒,模拟耗时操作
                            mPostHander.post(new Runnable() {

                                @Override
                                public void run() {
                                    // 更新UI
                                    mTextView.setText("通过Hander Post更新Ui");
                                }
                            });
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    };
                }.start();
                break;
            case R.id.update_mButton_02:// 直接通过Hander发送Message来更新UI
                new Thread() {
                    public void run() {
                        try {
                            sleep(2000);// 休眠2秒,模拟耗时操作
                            mPostHander.sendEmptyMessage(1);
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    };
                }.start();

                break;
            case R.id.update_mButton_03:// 通过runOnUiThread来实现ui更新
                new Thread() {
                    public void run() {
                        try {
                            sleep(2000);// 休眠2秒,模拟耗时操作
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    // 更新UI
                                    mTextView.setText("通过runOnUiThread更新Ui");
                                }
                            });
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    };
                }.start();

                break;
            case R.id.update_mButton_04:// 直接利用View.post方法来更新ui
                mTextView.post(new Runnable() {
                    @Override
                    public void run() {
                        // 更新UI
                        mTextView.setText("通过View.post()更新Ui");
                    }
                });
                break;
        }
    }
}
posted @ 2016-12-01 11:03  天涯海角路  阅读(197)  评论(0)    收藏  举报