android应用开发之ThreadHandler
来源:互联网 发布:西科软件靠谱吗 编辑:程序博客网 时间:2024/06/16 14:29
最近在复习下应用开发,直接上代码
ThreadHandlerActivity.java
package cn.sanbo.test.threadhandler;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class ThreadHandlerDownActivity extends Activity implementsView.OnClickListener {private TextView textView;private boolean isExits = false;private Handler handler = new Handler() {public void handleMessage(Message msg) {super.handleMessage(msg);int jd = (Integer) msg.obj;if (jd == 100) {textView.setText("耗时完成!");} else {textView.setText("耗时完成:" + jd + "%");}};};protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init() {this.findViewById(R.id.btnThread).setOnClickListener(this);textView = (TextView) this.findViewById(R.id.tv_content);}public void onClick(View v) {if (v.getId() == R.id.btnThread) {if (!isExits) {isExits = true;new MyThread().start();} else {Toast.makeText(ThreadHandlerDownActivity.this, "耗时操作进行中,请等待~",0).show();}}}class MyThread extends Thread {public void run() {super.run();boolean isGoOn = true;int jd = 0;while (isGoOn) {try {jd++;sleep(30);Message msg = new Message();msg.obj = jd;handler.sendMessage(msg);if (jd == 100) {isGoOn = false;}} catch (Exception e) {e.printStackTrace();}}}}}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/btnThread" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="threadhandler" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="down " android:textColor="@android:color/white" android:gravity="center" android:textSize="25sp" android:id="@+id/tv_content"/></LinearLayout>
- android应用开发之ThreadHandler
- Android Handler和ThreadHandler
- Android Handler和ThreadHandler
- android 线程(AsyncTask,Threadhandler,intentService)详解
- Android之应用开发
- Android开发之WebView应用
- Android 应用开发之vibrator
- Android应用开发之特性
- Android应用开发之SharedPreferes
- Android应用开发之ContentProvider
- Android应用开发之SurfaceView
- Android应用开发之国际化
- Android应用开发之样式
- Android应用开发之主题
- Android应用开发之电子相册
- Android开发之WebView应用
- Android应用开发之Menu
- android开发之应用编程
- 简单易用兼容IE6的Jquery遮盖层和弹框
- Unity 世界坐标转换到NGUI坐标
- UVLive 5061 LCA
- 开发者应该避免使用的6个Java功能
- test
- android应用开发之ThreadHandler
- ETF creation basket VS calculation basket
- 掌握 Dojo 工具包,第 2 部分: XHR 框架与 Dojo
- 屏驱动调试新规则记录
- mfc中socket字符接收不完整unicode问题
- PostGis
- 毫无编程能力的研一学生该如何安排
- 具备复制粘贴功能的JTextField和JTextArea
- opengl win32 nehe