900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Android 倒计时; 时分秒与秒数的互转

Android 倒计时; 时分秒与秒数的互转

时间:2024-04-12 20:04:42

相关推荐

Android 倒计时; 时分秒与秒数的互转

转载时请记得标明源地址:/lijindou/blog/798710

本人博客地址: /lijindou/blog

这篇博客主要是用Chronometer 实现了倒计时,如果又不想用这种方式实现的,我的另一篇博客中也有倒计时实现:

/lijindou/blog/714657

大家可以打开这个连接看看

在这篇博客中的倒计时很简单的,源码已经贴出来了,大家可看看就明白了,然后在下面有两个方法,

1.将时分秒转为秒数

2.将秒数转为时分秒

大家也看看吧!

源码地址:/detail/qq_33144317/9699730

MainActivity.java

import android.os.Bundle;import android.os.SystemClock;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Chronometer;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private static final String TAG = "MainActivity";private Chronometer timer;private Chronometer timer1;private Button but;private Button but1;private Button but2;private Button but3;private TextView tv0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();initData();addEvent();}private void initViews() {timer = (Chronometer) findViewById(R.id.timer);timer1 = (Chronometer) findViewById(R.id.timer1);but = (Button) findViewById(R.id.but);but1 = (Button) findViewById(R.id.but1);but2 = (Button) findViewById(R.id.but2);but3 = (Button) findViewById(R.id.but3);tv0 = (TextView) findViewById(R.id.tv0);}private void initData() {}private void addEvent() {but.setOnClickListener(this);but1.setOnClickListener(this);but2.setOnClickListener(this);but3.setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.but:but.setText("点击清零,从新开始 ");timer.setBase(SystemClock.elapsedRealtime());//计时器清零timer.start();break;case R.id.but1:but1.setText("点击清零,从新开始");timer1.setBase(SystemClock.elapsedRealtime());//计时器清零int hour = (int) ((SystemClock.elapsedRealtime() - timer1.getBase()) / 1000 / 60);timer1.setFormat("0" + String.valueOf(hour) + ":%s");timer1.start();break;case R.id.but2:if (but2.getText().toString().equals("点击暂停")) {timer1.stop();//计时停止但是不清零but2.setText("点击开始");} else {timer1.start();//计时停止但是不清零but2.setText("点击暂停");}break;case R.id.but3://Toast.makeText(this, + "", Toast.LENGTH_SHORT).show();tv0.setText("当前时间" + timer1.getText().toString() + "\n" +"当前总秒数: " + formatTurnSecond(timer1.getText().toString()) + "\n" +"当前总毫秒数: " + formatTurnSecond(timer1.getText().toString()) * 1000 + "\n" +"当前时间: " + change((int) formatTurnSecond(timer1.getText().toString())));break;}}/** 将时分秒转为秒数* */public long formatTurnSecond(String time) {String s = time;int index1 = s.indexOf(":");int index2 = s.indexOf(":", index1 + 1);int hh = Integer.parseInt(s.substring(0, index1));int mi = Integer.parseInt(s.substring(index1 + 1, index2));int ss = Integer.parseInt(s.substring(index2 + 1));Log.e(TAG, "formatTurnSecond: 时间== " + hh * 60 * 60 + mi * 60 + ss);return hh * 60 * 60 + mi * 60 + ss;}/** 将秒数转为时分秒* */public String change(int second) {int h = 0;int d = 0;int s = 0;int temp = second % 3600;if (second > 3600) {h = second / 3600;if (temp != 0) {if (temp > 60) {d = temp / 60;if (temp % 60 != 0) {s = temp % 60;}} else {s = temp;}}} else {d = second / 60;if (second % 60 != 0) {s = second % 60;}}return h + ":" + d + ":" + s + "";}}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="/apk/res/android"xmlns:tools="/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.time.MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Chronometerandroid:id="@+id/timer"android:layout_width="match_parent"android:layout_height="wrap_content"android:format="%s"android:gravity="center" /><Buttonandroid:id="@+id/but"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="点击开始" /><Chronometerandroid:id="@+id/timer1"android:layout_width="match_parent"android:layout_height="wrap_content"android:format="00:00:00"android:gravity="center" /><Buttonandroid:id="@+id/but1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="点击开始" /><Buttonandroid:id="@+id/but2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="点击暂停" /><Buttonandroid:id="@+id/but3"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="点击获取时间" /><TextViewandroid:id="@+id/tv0"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout></ScrollView>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。