带H:MM:SS的天文台表

时间:2016-07-07 05:15:01

标签: java android

如何用H:MM:SS显示天文台表?我读MM:SS和H:MM:SS默认显示。我只找到MM:SS。

这是我的代码:带有开始和停止按钮的MM:SS。

public class MainActivity extends AppCompatActivity {

Button start,stop;
Chronometer chrono;
long time;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    start = (Button) findViewById(R.id.start);
    stop = (Button) findViewById(R.id.stop);
    chrono = (Chronometer) findViewById(R.id.chronometer);

 start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           chrono.setBase(SystemClock.elapsedRealtime());


            chrono.start();
        }
    });

    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            chrono.stop();

        }
    });


}

}

这是我的MainActivity的xml代码:

    <Chronometer
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/chronometer"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:id="@+id/start"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stop"
    android:id="@+id/stop"
    android:layout_below="@+id/start"
    android:layout_centerHorizontal="true" />

3 个答案:

答案 0 :(得分:12)

  

H:MM:SS

的天文台表

使用 setOnChronometerTickListener将时间分为分钟,小时和秒

用这个......

Chronometer chrono  = (Chronometer) findViewById(R.id.chronomete);
chrono.setOnChronometerTickListener(new OnChronometerTickListener(){
        @Override
            public void onChronometerTick(Chronometer chronometer) {
            long time = SystemClock.elapsedRealtime() - chronometer.getBase();
            int h   = (int)(time /3600000);
            int m = (int)(time - h*3600000)/60000;
            int s= (int)(time - h*3600000- m*60000)/1000 ;
            String t = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
            chronometer.setText(t);
        }
    });
    chrono.setBase(SystemClock.elapsedRealtime());
    chrono.setText("00:00:00");

output

修改

开始

全局声明一个长变量timeWhenStopped维持时间。

private long timeWhenStopped = 0;

启动监听器... 获取时间,然后从那里开始。

 start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            chrono.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
            chrono.start();
        }
    }); 

停止听众.... 存储时间,当停止时,停止。

stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            timeWhenStopped = chrono.getBase() - SystemClock.elapsedRealtime();
            chrono.stop();

        }
    });

享受编码............

答案 1 :(得分:0)

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/tv_data"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Chronometer
    android:id="@+id/chronometer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv_data"
    android:layout_centerInParent="true" />

活动

import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tvData;
    Chronometer chronometer;

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

        tvData = (TextView) findViewById(R.id.tv_data);
        chronometer = (Chronometer) findViewById(R.id.chronometer);

        startChronometer();

        chronometer.setOnChronometerTickListener(new OnChronometerTickListener() {
            public void onChronometerTick(Chronometer cArg) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, getCurrentMiliSecondsOfChronometer());
                tvData.setText(DateFormat.format("HH:mm:ss", calendar.getTime()));
            }
        });
    }

    private int getCurrentMiliSecondsOfChronometer() {
        int stoppedMilliseconds = 0;
        String chronoText = chronometer.getText().toString();
        String array[] = chronoText.split(":");
        if (array.length == 2) {
            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000 + Integer.parseInt(array[1]) * 1000;
        } else if (array.length == 3) {
            stoppedMilliseconds =
                    Integer.parseInt(array[0]) * 60 * 60 * 1000 + Integer.parseInt(array[1]) * 60 * 1000
                            + Integer.parseInt(array[2]) * 1000;
        }
        return stoppedMilliseconds;
    }

    private void startChronometer() {
        int stoppedMilliseconds = getCurrentMiliSecondsOfChronometer();
        chronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
        chronometer.start();
    }
}

答案 2 :(得分:0)

您还可以设置有吸引力的 Chronometer 格式,例如 00小时:00分钟:00秒

        chronometer.setText("00 hrs : 00 min : 00 sec");
        chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {

            @Override
            public void onChronometerTick(Chronometer chronometer) {
                CharSequence text = chronometer.getText();
                if (text.length() == 5) {
                    String a[] = text.toString().split(":");
                    String min = a[0] + " min : ";
                    String sec = a[1] + " sec";
                    chronometer.setText("00 hrs : " + min + sec);
                } else if (text.length() == 7) {
                    String a[] = text.toString().split(":");
                    String hr = a[0] + " hrs : ";
                    String min = a[1] + " min : ";
                    String sec = a[2] + " sec";
                    chronometer.setText("0" + hr + min + sec);
                }
            }
        });