zoukankan      html  css  js  c++  java
  • 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);
      }
     }
    }

  • 相关阅读:
    Android weight属性详解
    设计模式(一)__单例设计模式
    Java中线程的生命周期
    抽象类和接口
    SQL sever 怎样将DBF文件导入到数据库
    JS去除字符串中空格,及常用正则表达式
    Oracle 11g问题1:关于error:ORA12541: TNS: 没有监听器
    access、excel取随机n条记录
    tsql字符串操作
    测试SQL Server执行时间和CPU时间
  • 原文地址:https://www.cnblogs.com/xsmhero/p/2532857.html
Copyright © 2011-2022 走看看