安卓开发笔记(十九):异步消息处理机制实现更新软件UI
2019-03-31 21:07
639 查看
版权声明:本文章由Geek Song创造,转载请注明链接,作者。否则必追求法律责任。 https://blog.csdn.net/Geeksongs/article/details/89038422
安卓开发笔记(十九):异步消息处理机制实现更新软件UI
主界面代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/change" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="改变内容"/> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="你好,世界!" android:textSize="20sp"/> </RelativeLayout>
主活动代码:
import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private TextView text; public static final int UPDATE_TEXT=1; private Handler handler=new Handler(){ public void handleMessage(Message msg){ switch (msg.what){ case UPDATE_TEXT: text.setText("遇见你真好"); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text=(TextView)findViewById(R.id.text); Button ChangeText=(Button)findViewById(R.id.change); ChangeText.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.change: new Thread(new Runnable() { @Override public void run() { Message message=new Message(); message.what=UPDATE_TEXT; handler.sendMessage(message); } }).start(); break; default: break; } } }
posted @ 2019-03-31 21:07 Geeksongs 阅读(...) 评论(...) 编辑 收藏
相关文章推荐
- 安卓开发之异步消息处理机制
- Android:在子线程中更新UI,解析异步消息处理机制(Handler)
- 安卓异步消息处理机制ALooperRoster
- Android异步更新UI的方式之使用Handler消息传递机制
- 使用异步消息机制在子线程中更新UI
- 笔记:安卓App消息处理机制
- 安卓异步消息处理机制
- Android应用开发学习笔记之多线程与Handler消息处理机制
- 安卓UI线程与异步消息处理机制
- iOS学习笔记---利用NSThread实现异步更新UI和下载图片
- 安卓异步消息处理机制
- 在子线程中更新UI+延时更新(异步消息处理机制)
- Android笔记----AsyncTask异步消息处理机制
- Android的异步消息处理机制笔记
- 安卓笔记之网络图片查看器小程序以及消息处理机制运用
- 安卓异步消息处理机制源码流程
- Android消息处理机制:源码剖析Handler、Looper,并实现图片异步加载
- Android开发之异步消息处理机制AsyncTask
- 安卓开发笔记(二十七):实现轮播文字消息
- Android开发总结笔记 消息处理机制 5-1