国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現使用handler在子線程中更新UI示例

2019-10-22 18:27:05
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現使用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>

 

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 池州市| 闽清县| 扎赉特旗| 山阴县| 井陉县| 乐安县| 迁安市| 西青区| 丹凤县| 隆昌县| 麻江县| 云南省| 任丘市| 毕节市| 通许县| 家居| 西平县| 司法| 贵溪市| 旺苍县| 松桃| 开原市| 寿宁县| 罗田县| 蒲城县| 睢宁县| 龙江县| 栾城县| 札达县| 溆浦县| 且末县| 开远市| 巴彦淖尔市| 宿迁市| 合山市| 耿马| 于田县| 白朗县| 巫山县| 柳州市| 湄潭县|