当前位置: 代码迷 >> Android >> 求教关于timer的有关问题
  详细解决方案

求教关于timer的有关问题

热度:37   发布时间:2016-05-01 20:51:34.0
求教关于timer的问题
Java code
package Y.B;import java.util.ArrayList;import java.util.List;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;public class stopwatchActivity extends Activity implements OnClickListener {    RadioGroup rg;    RadioButton rbfenbei, rbyiquan;    ListView lvjilu;    TextView tvxianshi;    Button btnjishi, btnkaishi;    Timer timer = new Timer();    TimerTask task = new TimerTask() {        @Override        public void run() {            try {                Message msg = handler.obtainMessage();                msg.what = 0;                run[3]++;                if(run[3] == 100) {                    run[3] = 0;                    run[2]++;                }                if(run[2] == 60) {                    run[2] = 0;                    run[1]++;                }                if(run[1] == 60) {                    run[1] = 0;                    run[0]++;                }                Thread.sleep(10);                msg.obj = run[3];                msg.sendToTarget();            }            catch(Exception e) {                e.printStackTrace();            }        }    };    List<String> jilu = new ArrayList<String>();    ArrayAdapter<String> jilus;    Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            switch(msg.what) {                case 0:// 显示时间                    tvxianshi.setText((run[0] < 10 ? "0" + run[0] : run[0])                            + ":" + (run[1] < 10 ? "0" + run[1] : run[1]) + ":"                            + (run[2] < 10 ? "0" + run[2] : run[2]) + ":"                            + (run[3] < 10 ? "0" + run[3] : run[3]));                    break;            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.stopwatch);        rg = (RadioGroup) findViewById(R.id.rg);        rbfenbei = (RadioButton) findViewById(R.id.rbfenbie);        rbyiquan = (RadioButton) findViewById(R.id.rbyiquan);        lvjilu = (ListView) findViewById(R.id.lvjilu);        tvxianshi = (TextView) findViewById(R.id.tvxianshi);        btnjishi = (Button) findViewById(R.id.btnjishi);        btnkaishi = (Button) findViewById(R.id.btnkaishi);        btnkaishi.setOnClickListener(this);        btnjishi.setOnClickListener(this);        jilus = new ArrayAdapter<String>(stopwatchActivity.this,                android.R.layout.simple_list_item_1, jilu);        lvjilu.setAdapter(jilus);    }    boolean isRun = false;// 是否在计时    int[] run = { 0, 0, 0, 0 };// 表示时间的时、分、秒、毫秒    @Override    public void onClick(View v) {        switch(v.getId()) {            case R.id.btnkaishi:                if(!isRun) {                    timer.schedule(task, 10, 10);                    isRun = true;                }else{}                break;            case R.id.btnjishi:                int[] one = run.clone();                String myjilu = (one[0] < 10 ? "0" + one[0] : one[0]) + ":"                        + (one[1] < 10 ? "0" + one[1] : one[1]) + ":"                        + (one[2] < 10 ? "0" + one[2] : one[2]) + ":"                        + (one[3] < 10 ? "0" + one[3] : one[3]);                jilu.add(myjilu);                jilus.notifyDataSetChanged();                break;        }    }}
  相关解决方案