在计时器中显示毫秒数

时间:2014-09-21 13:26:54

标签: java android eclipse sdk

我在eclipse中为android制作秒表。我正在使用天文台,我想知道如何显示毫秒。这就是我的代码的样子。 P.S:我是初学者,所以这对你来说听起来很愚蠢:P

       Button startChrono;
Button pauseChrono;
Chronometer chrono;
long time = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startChrono = (Button) findViewById(R.id.button1);
    pauseChrono = (Button) findViewById(R.id.button2);
    chrono = (Chronometer) findViewById(R.id.chronometer1);
    startChrono.setOnClickListener(this);
    pauseChrono.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.button1:
        chrono.setBase(SystemClock.elapsedRealtime() + time);
        chrono.start();
        break;

    case R.id.button2:
        time = chrono.getBase() - SystemClock.elapsedRealtime();
        chrono.stop();
        break;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为用计时器不可能实现这一点。它仅支持细粒度的时间分辨率,最低可达秒。此外,在查看代码后,我发现他们正在这样做:

private Handler mHandler = new Handler() {
    public void More ...handleMessage(Message m) {
        if (mRunning) {
            updateText(SystemClock.elapsedRealtime());
            dispatchChronometerTick();
            sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000);
        }
    }
};

看起来它是故意的,它目前每秒只更新一次。这是可以理解的,因为此组件是TextView,并且通常会在文本更改时导致其父组件的重新布局(如果layout_width是wrap_parent。)

您必须寻找替代解决方案,以获得所需的详细程度和更新性能。