Android秒表:以毫秒为单位的时间不起作用

时间:2015-02-12 15:35:01

标签: java widget stopwatch milliseconds chronometer

我知道,我知道,这里有很多关于这个特殊问题的帖子,但是,大多数都非常过时,而且没有一个符合我写这个应用程序的方式......我很新编码,我自己这样做,所以我需要帮助,告诉我如何使用我拥有的,并让计时器显示毫秒!

这是我的Java:

package com.jackson.eason.stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;

public class MainActivity extends Activity {

    Chronometer mChronometer;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get references to views
        mChronometer = (Chronometer) findViewById(R.id.chronometer);
        Button button1 = (Button) findViewById(R.id.start);
        Button button2 = (Button) findViewById(R.id.stop);
        Button button3 = (Button) findViewById(R.id.reset);
        Button button4 = (Button) findViewById(R.id.set_format);
        Button button5 = (Button) findViewById(R.id.clear_format);



        // Declare the listeners
        View.OnClickListener mStartListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.start();
            }
        };

        View.OnClickListener mStopListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.stop();
            }
        };

        View.OnClickListener mResetListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.setBase
                        (SystemClock.elapsedRealtime());
            }
        };

        // You forgot to declare a listener for set format in your updated code
        View.OnClickListener mSetFormatListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.setFormat("Formatted time (%s)");
            }
        };

        View.OnClickListener mClearFormatListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.setFormat(null);
            }
        };
        //Assign listeners to your buttons
        button1.setOnClickListener(mStartListener);
        button2.setOnClickListener(mStopListener);
        button3.setOnClickListener(mResetListener);
        button4.setOnClickListener(mSetFormatListener);
        button5.setOnClickListener(mClearFormatListener);
    }




}

此外,如果需要,这是我的主XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:clickable="true"
    android:background="#ff4f4a53">

    <Chronometer android:id="@+id/chronometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="30dip"
        android:paddingTop="30dip"
        android:focusableInTouchMode="true"
        android:textSize="55sp"
        android:textIsSelectable="false"
        android:typeface="serif"
        android:textColor="#fffcfffa" />

    <Button android:id="@+id/start"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="start"
        android:background="#ff5effab"
        android:elegantTextHeight="false"
        android:layout_gravity="center_horizontal">
        <requestFocus />
    </Button>

    <Button android:id="@+id/stop"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="stop"
        android:background="#fffe7c5b">
    </Button>

    <Button android:id="@+id/reset"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="reset"
        android:layout_gravity="center_horizontal"
        android:textColor="#fffffcfe">
        </Button>

    <Button android:id="@+id/clear_format"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="clear"
        android:layout_gravity="center_horizontal"
        android:textColor="#fffefdff">
    </Button>

    <Button android:id="@+id/set_format"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="format"
        android:layout_gravity="center_horizontal"
        android:textColor="#ffffffff">
    </Button>


</LinearLayout>

非常感谢任何帮助!

0 个答案:

没有答案