Android(安卓)使用线程更新UI(附带计时器demo)
16lz
2021-01-26
上一篇写到两种使用线程简单实用的方法,那么使用线程更新UI,只要使用得当还是可以的。虽然说更新UI需要主线程,请看下面解说:用线程更新UI。
下载地址:http://download.csdn.net/download/qq_16064871/8499931
转载请注明出处:http://blog.csdn.net/qq_16064871
package com.example.thanderdemo;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.annotation.SuppressLint;import android.app.Activity;import android.view.View;import android.widget.TextView;import android.widget.Toast;@SuppressLint("HandlerLeak")public class MainActivity extends Activity {private TimeManage mTimeManage; // 计时管理类private TextView mtvTime;private int mntime = -1;// 更新界面的线程private Handler mHandlerUI = new Handler() {public void handleMessage(Message msg) {Bundle dataBundle = msg.getData();if (dataBundle == null)return;mntime = dataBundle.getInt("time");if (mntime >= 0)mtvTime.setText(String.valueOf(mntime));}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mtvTime = (TextView) findViewById(R.id.tv_time);mTimeManage = new TimeManage(mHandlerUI);}// 按键开始监听public void Start(View v) {if (mntime > 0) {Toast.makeText(getApplicationContext(), "已经开始计时....",Toast.LENGTH_LONG).show();} else {mTimeManage.StaetTime();}}// 按键结束监听public void End(View v) {mTimeManage.EndTime();}}
代码上有注释,看代码吧。
TimeManage类
package com.example.thanderdemo;import android.os.Bundle;import android.os.Handler;import android.os.Message;public class TimeManage {// handler句柄Handler mHandler;private int i = 0;public TimeManage(Handler handler) {mHandler = handler;handler.post(updateThread); // 启动线程}Handler handler = new Handler();// 将要执行的操作写在线程对象的run方法当中Runnable updateThread = new Runnable() {public void run() {//停留1秒handler.postDelayed(updateThread, 1000);OnReceiveTime(i);i++;}};// 开始计时public void StaetTime() {if (i == 0) {handler.post(updateThread); // 启动线程}}// 结束计时public void EndTime() {handler.removeCallbacks(updateThread);i = 0;OnReceiveTime(i);}// 用句柄hander返回数据时,有时需要封装一下数据。// 用Bundle封装。不能用msg.obj直接返回数据。更新。public void OnReceiveTime(int ntime) {Message msg = new Message();Bundle dataBundle = new Bundle();dataBundle.putInt("time", ntime);msg.setData(dataBundle);mHandler.sendMessage(msg);}}
顺便贴上xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center" > <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="66sp" android:text="0" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="Start" android:text="开始" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="End" android:text="结束" /> </LinearLayout></LinearLayout>
欢迎指正讨论。
附带这上写的计时器项目:
下载地址:http://download.csdn.net/download/qq_16064871/8499931
转载请注明出处:http://blog.csdn.net/qq_16064871
更多相关文章
- 倒计时效果
- SDK is not loaded yet解决方法
- [Android]Ultra-Pull-To-Refresh之listview下拉刷新、上拉加载的
- 【Android游戏开发十五】关于Android(安卓)游戏开发中 OnTouchEv
- Android(安卓)Studio 1.2正式版发布,要如何更新? Please check net
- Android异步处理类AsyncTask
- android andbase 线程池
- Eclispe中的ArcGIS Android(安卓)SDK更新地址发生改变
- android 点击获取验证码显示倒计时并不可用