Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android——線程+最簡單計數器

Android——線程+最簡單計數器

編輯:關於Android編程

1.第一種方法

package com.example.jer824;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private TextView tv1;
    int a=100;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==1){
                Log.d("??????",a+"");
                tv.setText(a+"");
                a--;
                Log.d("??????","成功");
             new  GetCache(a).run();

            }else{tv.setText("倒計時結束");}
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     tv=(TextView)findViewById(R.id.tv);
        tv1=(TextView)findViewById(R.id.tv1);
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new  GetCache(a).run();
            }
        });

    }

    private class GetCache implements Runnable {  //兩種方法繼承或接口
        private int id;

        public GetCache(int id) {

            this.id = id;
        }

        @Override
        public void run() {



            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Message message = new Message();
            if (id==0) {
                message.what = 2;
                handler.sendMessage(message);
            }else {
                message.what = 1;
                handler.sendMessage(message);
            }
        }
    }
}




    
    


2.第二種方法

 

 

package com.example.jer824;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {
    private TextView tv;
    private Button bt;
    //線程間通信
    private android.os.Handler handler=new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            tv.setText(msg.what+"");

            Bundle bundle=msg.getData();
            String str=bundle.getString("a");
            tv.setText(str+msg.what);

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tv= (TextView) findViewById(R.id.tv);
        bt= (Button) findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new  Thread(new GetCache()).start();


//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        int a=100;
//                        while (a>=0){
//                            try {
//                                Thread.sleep(100);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                            a--;
//                            Message message=new Message();
//                            message.what = a;
//                            handler.sendMessage(message);
//                        }
//                    }
//                }).start();
            }
        });
    }
    //多線程 子線程
    //public class GetCache extends Thread
    public class GetCache implements Runnable{
        @Override
        public void run() {
            //子線程運行完了,去主線程
                         int a=100;
                        while (a>=0){
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            a--;
                            Message message=new Message();
                            message.what = a;
                            Bundle bundle=new Bundle();
                            bundle.putString("a","AAA");
                            message.setData(bundle);

                         //   handler.sendMessageDelayed(message,200);

                          handler.sendMessage(message);
                         //與上面三行一個意思
                          //  handler.sendEmptyMessage(a);


                        }
        }
    }

}


    

3.第三種方法
package com.example.jer824;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Main3Activity extends AppCompatActivity {
    private TextView tv;
    private Button bt;
    private int count=100;
    //線程間通信
    private android.os.Handler handler=new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //tv.setText(msg.what);
            if(count==0){
                return;
            }
            tv.setText(msg.what + "");
            handler.sendEmptyMessageDelayed(count--,500);

            //tv.setText(msg.what + "");
            //bundle傳值
//            Bundle bundle=msg.getData();
//            String str = bundle.getString("a");
//            tv.setText(str+msg.what);


        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tv= (TextView) findViewById(R.id.tv);
        bt= (Button) findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(count);
//
//            new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        int a=100;
//                        while (a>0){
//                            try {
//                                Thread.sleep(100);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                            a--;
//                           // tv.setText(a+"");
//                            Message message=new Message();
//                            message.what = a;
//                            handler.sendMessage(message);
//                        }
//                    }
//                }).start();
                //new Thread(new GetCache()).start();
            }
        });
    }
    //多線程 子線程
    //public class GetCache extends Thread
    public class GetCache implements Runnable{
        @Override
        public void run() {
            //子線程運行完了,去主線程
            int a=100;
            while (a>0){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                a--;
                // tv.setText(a+"");
                Message message=new Message();
                message.what = a;
                //加入bundle值
//                Bundle bundle=new Bundle();
//                bundle.putString("a","1");
//                message.setData(bundle);
                handler.sendMessage(message);
                //多久後啟動
                //handler.sendMessageAtTime(message,500);
                //handler.sendMessageDelayed(message,500);

                //handler.sendEmptyMessage(a);傳空值
            }

        }
    }

}


    

\
\

 

\

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved