Handler详解系列(六)——View的post()方法详解
来源:互联网 发布:口算生成器 软件下载 编辑:程序博客网 时间:2024/06/06 20:11
MainActivity如下:
main.xml如下:
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" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test" android:layout_centerHorizontal="true" android:layout_marginTop="50dip" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_centerHorizontal="true" android:layout_marginTop="120dip" /> <TextView android:id="@+id/tipTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试在子线程中更新UI" android:layout_centerInParent="true" /></RelativeLayout>
1 0
- Handler详解系列(六)——View的post()方法详解
- Handler详解系列(五)——Handler的post()方法详解
- view.post,handler.runonUiThread详解
- Android中的Handler的post方法详解
- Android中的Handler的post方法详解
- Android--Handler的post方法详解
- Android中的Handler的post方法详解
- Android中的Handler的post方法详解
- Android中的Handler的post方法详解
- Android中的Handler的post方法详解
- Handler详解系列(九)——Handler常用方法示例
- Handler详解系列(七)——Activity.runOnUiThread()方法详解
- 关于Handler的post跟View的post方法。
- 处女男学Android(五)---Handler完结篇之Handler的post方法详解
- 【ES系列六】——配置文件详解
- handler——Handler详解
- Handler详解系列(一)——Handler异步消息机制详解(附图)
- Android的POST方法详解
- ACM Meteor Shower(挑战程序设计竞赛)
- 聚内核与微内核
- 第七周项目3(1)-求两数正差值
- uva-301 - Transportation
- 怎么使用jquery阻止页面的离开或卸载
- Handler详解系列(六)——View的post()方法详解
- 常见开源加密算法库
- 【Linux】调整ubuntu10.04 窗口关闭按钮的位置
- 2014-10-12 学习日志
- JAVA中properties配置文件的两种读取方式
- 羽毛球业余分级新标准
- Handler详解系列(四)——利用Handler在主线程与子线程之间互发消息
- GPSTime To UTC Date Time
- 第七周项目二并联电阻