Handler详细说明系列(六)——View的post()详解
生活随笔
收集整理的這篇文章主要介紹了
Handler详细说明系列(六)——View的post()详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MainActivity例如下列:
package cc.testui2; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.app.Activity; /*** Demo描寫敘述:* 在子線程中更改UI的方式二* * 在子線程中採用View的post()方法.* 根據源代碼可知它在終于還是調用了UI線程的handler的post()方法.* 所以在post方法中勿做耗時操作* * 看一下該方法的源代碼:** Causes the Runnable to be added to the message queue.* The runnable will be run on the user interface thread.** @param action The Runnable that will be executed.** @return Returns true if the Runnable was successfully placed in to the* message queue. Returns false on failure, usually because the* looper processing the message queue is exiting.** public boolean post(Runnable action) {* final AttachInfo attachInfo = mAttachInfo;* if (attachInfo != null) {* return attachInfo.mHandler.post(action);* }* // Assume that post will succeed later* ViewRootImpl.getRunQueue().post(action);* return true;* }* * * 該方法的英文凝視:* 將Runnable放入消息隊列,并在UI線程中運行.* * 參考資料:* http://blog.csdn.net/guolin_blog/article/details/9991569* Thank you very much*/public class MainActivity extends Activity {private TextView mTextView;private TextView mTipTextView;private Button mButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init(){mTextView=(TextView) findViewById(R.id.textView);mTipTextView=(TextView) findViewById(R.id.tipTextView);mButton=(Button) findViewById(R.id.button);mButton.setOnClickListener(new OnClickListenerImpl());System.out.println("UI線程ID="+Thread.currentThread().getId());}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {mTipTextView.post(new Runnable() {@Overridepublic void run() {mTextView.setText("My number is 9527");System.out.println("view.post(new Runnable())里的run()方法 線程ID="+Thread.currentThread().getId());}});}} }
main.xml例如以下:
<RelativeLayout 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"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="test"android:layout_centerHorizontal="true"android:layout_marginTop="50dip"/><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button"android:layout_centerHorizontal="true"android:layout_marginTop="120dip"/><TextViewandroid:id="@+id/tipTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="測試在子線程中更新UI" android:layout_centerInParent="true"/></RelativeLayout>
package cc.testui2; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.app.Activity; /*** Demo描寫敘述:* 在子線程中更改UI的方式二* * 在子線程中採用View的post()方法.* 根據源代碼可知它在終于還是調用了UI線程的handler的post()方法.* 所以在post方法中勿做耗時操作* * 看一下該方法的源代碼:** Causes the Runnable to be added to the message queue.* The runnable will be run on the user interface thread.** @param action The Runnable that will be executed.** @return Returns true if the Runnable was successfully placed in to the* message queue. Returns false on failure, usually because the* looper processing the message queue is exiting.** public boolean post(Runnable action) {* final AttachInfo attachInfo = mAttachInfo;* if (attachInfo != null) {* return attachInfo.mHandler.post(action);* }* // Assume that post will succeed later* ViewRootImpl.getRunQueue().post(action);* return true;* }* * * 該方法的英文凝視:* 將Runnable放入消息隊列,并在UI線程中運行.* * 參考資料:* http://blog.csdn.net/guolin_blog/article/details/9991569* Thank you very much*/public class MainActivity extends Activity {private TextView mTextView;private TextView mTipTextView;private Button mButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init(){mTextView=(TextView) findViewById(R.id.textView);mTipTextView=(TextView) findViewById(R.id.tipTextView);mButton=(Button) findViewById(R.id.button);mButton.setOnClickListener(new OnClickListenerImpl());System.out.println("UI線程ID="+Thread.currentThread().getId());}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {mTipTextView.post(new Runnable() {@Overridepublic void run() {mTextView.setText("My number is 9527");System.out.println("view.post(new Runnable())里的run()方法 線程ID="+Thread.currentThread().getId());}});}} }
main.xml例如以下:
<RelativeLayout 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"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="test"android:layout_centerHorizontal="true"android:layout_marginTop="50dip"/><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button"android:layout_centerHorizontal="true"android:layout_marginTop="120dip"/><TextViewandroid:id="@+id/tipTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="測試在子線程中更新UI" android:layout_centerInParent="true"/></RelativeLayout>
版權聲明:本文博客原創文章。博客,未經同意,不得轉載。
轉載于:https://www.cnblogs.com/zfyouxi/p/4746153.html
總結
以上是生活随笔為你收集整理的Handler详细说明系列(六)——View的post()详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL当您插入列无效的数据插入
- 下一篇: poj 3461 Oulipo(kmp统