Android編程實(shí)現(xiàn)使用handler在子線程中更新UI示例
本文實(shí)例講述了Android編程實(shí)現(xiàn)使用handler在子線程中更新UI。分享給大家供大家參考,具體如下:
MainActivity代碼:
package com.example.ui;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
new Thread(){
@Override
public void run() {
super.run();
try {
Thread.sleep(2000);
handler.post(new Runnable() {
@Override
public void run() {
textView.setText("ok");
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ui.MainActivity">
<TextView
android:textSize="40sp"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android線程與消息機(jī)制用法總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android實(shí)現(xiàn)簡(jiǎn)易鬧鐘功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
Flutter基本組件Basics?Widget學(xué)習(xí)
本文詳細(xì)講解了Flutter基本組件Basics?Widget,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
android實(shí)現(xiàn)關(guān)閉或開(kāi)啟移動(dòng)網(wǎng)絡(luò)數(shù)據(jù)
本篇文章是對(duì)android實(shí)現(xiàn)關(guān)閉或開(kāi)啟移動(dòng)網(wǎng)絡(luò)數(shù)據(jù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解
這篇文章主要介紹了Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10
Android:下拉刷新+加載更多+滑動(dòng)刪除實(shí)例講解
本文主要講解 Android下拉刷新+加載更多+滑動(dòng)刪除的示例,這里整理了相關(guān)資料并附示例代碼供大家學(xué)習(xí)參考,有需要的小伙伴可以參考下2016-08-08
Android實(shí)現(xiàn)左滑關(guān)閉窗口
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)左滑關(guān)閉窗口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android自定義控件實(shí)現(xiàn)雷達(dá)圖效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)雷達(dá)圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09

