android 系统倒计时显示时间,Android 依据系统时间整点、半点倒计时
activity_main.xml中的代碼
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
>
android:id="@+id/mytime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="36sp"/>
MainActivity.java主文件中的代碼
package com.example.time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int msgKey1 = 1;
private TextView mTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTime = (TextView) findViewById(R.id.mytime);
new TimeThread().start();
}
public class TimeThread extends Thread {
@Override
public void run () {
do {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = msgKey1;
mHandler.sendMessage(msg);
}
catch (InterruptedException e) {
e.printStackTrace();
}
} while(true);
}
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage (Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case msgKey1:
//獲取分鐘數
long sysTime = System.currentTimeMillis();
String mmtime = (String) DateFormat.format("mm", sysTime);
int i = Integer.parseInt(mmtime);
//倒計時
SimpleDateFormat sdfStopTime = new SimpleDateFormat("mm:ss", Locale.ENGLISH);
if(i < 30){
String newStoptime = sdfStopTime.format(new Date(30 * 60 * 1000 - System.currentTimeMillis() ));
mTime.setText("倒計時:"+newStoptime);
break;
}else{
String newStoptime = sdfStopTime.format(new Date(60 * 60 * 1000 - System.currentTimeMillis() ));
mTime.setText("倒計時:"+newStoptime);
break;
}
default:
break;
}
}
};
}
總結
以上是生活随笔為你收集整理的android 系统倒计时显示时间,Android 依据系统时间整点、半点倒计时的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: %matplotlib qt5 作用
- 下一篇: [转]FPGA使用LVDS差分信号的一些