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)容感興趣的讀者可查看本站專題:《Android線程與消息機(jī)制用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android訪問php取回json數(shù)據(jù)實(shí)例
Android訪問php取回json數(shù)據(jù),實(shí)現(xiàn)代碼如下,遇到訪問網(wǎng)絡(luò)的權(quán)限不足在AndroidManifest.xml中,需要進(jìn)行如下配置2013-06-06Android多點(diǎn)觸控實(shí)現(xiàn)對(duì)圖片放大縮小平移,慣性滑動(dòng)等功能
這篇文章主要介紹了Android多點(diǎn)觸控實(shí)現(xiàn)對(duì)圖片放大縮小平移,慣性滑動(dòng)等功能的相關(guān)資料,需要的朋友可以參考下2016-02-02Android spinner下垃菜單用法實(shí)例詳解
這篇文章主要介紹了Android spinner下垃菜單用法,詳細(xì)分析了spinner下垃菜單的定義、布局及功能實(shí)現(xiàn)相關(guān)技巧,需要的朋友可以參考下2016-07-07android Imageview 圖片覆蓋具體實(shí)現(xiàn)
android Imageview 圖片覆蓋實(shí)現(xiàn)及注意事項(xiàng)如下,感興趣的朋友可以參考下哈2013-06-06Android實(shí)現(xiàn)九宮格橫向左右滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)九宮格橫向左右滑動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android編程實(shí)現(xiàn)異步消息處理機(jī)制的幾種方法總結(jié)
這篇文章主要介紹了Android編程實(shí)現(xiàn)異步消息處理機(jī)制的幾種方法,結(jié)合實(shí)例形式詳細(xì)總結(jié)分析了Android異步消息處理機(jī)制的原理、相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2018-08-08