Android 线程

package cn.xsm.hero;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;

public class AndroidThreadActivity extends Activity {
 /** Called when the activity is first created. */
 private Button buttonOk;
 private EditText editTextRsult;
 private int counts = 0;
 public Handler handler;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  editTextRsult = (EditText) this.findViewById(R.id.editTextResult);
  buttonOk = (Button) this.findViewById(R.id.buttonOk);
  buttonOk.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    counts++;
    getResult();
   }

  });

  handler = new Handler() {
   @Override
   public void handleMessage(Message msg) {
    if (msg.what == 0) {
     String v = (String) msg.obj;
     String ss = editTextRsult.getText().toString();
     editTextRsult.setText(ss + " -- " + v);
    }
   }
  };

 }

 public void getResult() {
  // 启动文件下载线程
  new MyThread(counts).start();
 }

 public class MyThread extends Thread {
  private int counts = 0;

  public MyThread(int c) {
   counts = c;
  }

  @Override
  public void run() {
   try {
    Thread.sleep(2000);
   } catch (Exception e) {
   }
   Message msg = Message.obtain();
   msg.what = 0;
   msg.obj = Integer.toString(counts);
   handler.sendMessage(msg);
  }
 }
}

posted @ 2012-06-03 17:39  小さいです哥  阅读(146)  评论(0编辑  收藏  举报