一个handle使用更新线程的实例

handle更新线程实例

package com.example.administrator.handle;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView text_view = null;
private Button start = null;
private Button end = null;
//使用Runable 接口启动新线程的步骤
//1.建立runable对象,runnable是个接口,要编写实现runnable接口的类:Runnable update_thread = new Runnable()
//runna只有一个方法,且为public属性;
//2.建立一个Thread 对象,并将实现runnable接口的类的对象作为参数传入到thread中;Thread t = new Thread(r);
//3.调用start方法启动线程:t.start();
//使用handler时首先要创建一个handler
Handler handler = new Handler();
//要用handler来处理多线程可以使用runnable接口,这里先定义该接口
//线程中运行该接口的run函数
Runnable update_thread = new Runnable()
{
public void run()
{
//线程每次执行时输出"UpdateThread..."文字,且自动换行
//textview的append功能和Qt中的append类似,不会覆盖前面
//的内容,只是Qt中的append默认是自动换行模式
//append 意思是在textview后面追加内容
text_view.append("\nUpdateThread...");
//延时1s后又将线程加入到多线程队列中,实现1s一次的定时器操作;
handler.postDelayed(update_thread, 1000);

}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text_view = (TextView)findViewById(R.id.text_view);
start = (Button)findViewById(R.id.start);
start.setOnClickListener(new StartClickListener());
end = (Button)findViewById(R.id.end);
end.setOnClickListener(new EndClickListener());

}
private class StartClickListener implements OnClickListener
{
public void onClick(View v) {
// TODO Auto-generated method stub
//将一个线程加入到线程队列
handler.post(update_thread);
}
}

private class EndClickListener implements OnClickListener
{
public void onClick(View v) {
// TODO Auto-generated method stub
//将一个线程从线程队列中移除

handler.removeCallbacks(update_thread);
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="vertical" >

<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/start"
/>
<Button
android:id="@+id/end"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/end"
/>

</LinearLayout>

转载于:https://www.cnblogs.com/xhx503/p/9995061.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值