Android多个倒计时,以及chronometer使用,在ExecutorService线程中执行

package com.example.administrator.listener;

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

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

    Button button;
    TextView mTextView;
    TextView mTextView1;
    int q = 11;
    private int i = 13;
    private int ii = 13;

    Chronometer chronometer;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (i > 0) {
                        i--;
                        mTextView.setText("" + i);
                        handler.sendEmptyMessageDelayed(0, 1000);
                    } else {

                    }

                    break;
                case 1:
                    if (ii > 0) {
                        ii--;
                        mTextView1.setText("" + ii);
                        handler.sendEmptyMessageDelayed(1, 1000);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.bt_ceshi);
        chronometer = findViewById(R.id.chronometer);
        chronometer.start();

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        executorService.execute(new Runnable() {
            public void run() {
                handler.sendEmptyMessageDelayed(0, 1000);
                handler.sendEmptyMessageDelayed(1, 1000);

            }
        });
        executorService.execute(new Runnable() {
            public void run() {
                handler.sendEmptyMessageDelayed(1, 1000);
            }
        });
        mTextView = findViewById(R.id.tv);
        mTextView1 = findViewById(R.id.tv1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MainActivityTwo.class);
                startActivity(intent);
                MainActivityTwo mainActivityTwo = new MainActivityTwo();
            }
        });
    }
}

你可能感兴趣的:(实达)