Android實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能展示的具體代碼,供大家參考,具體內(nèi)容如下
效果展示

MainActivity(主頁面代碼)
public class MainActivity extends Activity {
private RelativeLayout countDown;
// 倒計(jì)時(shí)
private TextView daysTv, hoursTv, minutesTv, secondsTv;
private long mDay = 10;
private long mHour = 10;
private long mMin = 30;
private long mSecond = 00;// 天 ,小時(shí),分鐘,秒
private boolean isRun = true;
private Handler timeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1) {
computeTime();
daysTv.setText(mDay+"");
hoursTv.setText(mHour+"");
minutesTv.setText(mMin+"");
secondsTv.setText(mSecond+"");
if (mDay==0&&mHour==0&&mMin==0&&mSecond==0) {
countDown.setVisibility(View.GONE);
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDown = (RelativeLayout) findViewById(R.id.countdown_layout);
daysTv = (TextView) findViewById(R.id.days_tv);
hoursTv = (TextView) findViewById(R.id.hours_tv);
minutesTv = (TextView) findViewById(R.id.minutes_tv);
secondsTv = (TextView) findViewById(R.id.seconds_tv);
startRun();
}
/**
* 開啟倒計(jì)時(shí)
*/
private void startRun() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (isRun) {
try {
Thread.sleep(1000); // sleep 1000ms
Message message = Message.obtain();
message.what = 1;
timeHandler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
/**
* 倒計(jì)時(shí)計(jì)算
*/
private void computeTime() {
mSecond--;
if (mSecond < 0) {
mMin--;
mSecond = 59;
if (mMin < 0) {
mMin = 59;
mHour--;
if (mHour < 0) {
// 倒計(jì)時(shí)結(jié)束
mHour = 23;
mDay--;
}
}
}
}
}
main(主頁面布局)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/countdown_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:gravity="center" > <RelativeLayout android:id="@+id/daojishi_rl" android:layout_width="match_parent" android:layout_height="40.0dip" android:layout_marginLeft="10.0dip" android:layout_marginRight="10.0dip" android:gravity="center" > <ImageView android:id="@+id/describe_iv" android:layout_width="40.0dip" android:layout_height="40.0dip" android:background="@drawable/clock" android:gravity="center_vertical" /> <TextView android:id="@+id/describe_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginRight="5.0dip" android:layout_toRightOf="@+id/describe_iv" android:text="距離活動(dòng)還有 " android:textSize="25sp" /> <TextView android:id="@+id/days_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/describe_tv" android:textColor="@color/colorAccent" android:gravity="center" android:text="20" android:textSize="20sp" /> <TextView android:id="@+id/colon0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5.0dip" android:layout_marginRight="3.0dip" android:layout_toRightOf="@+id/days_tv" android:text="天" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/daojishi_rl" android:gravity="center_horizontal" > <TextView android:id="@+id/hours_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/colon1" android:textColor="@color/colorAccent" android:gravity="center" android:text="23" android:textSize="20sp" /> <TextView android:id="@+id/colon1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="3.0dip" android:layout_marginRight="3.0dip" android:layout_toLeftOf="@+id/minutes_tv" android:text=":" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/minutes_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/colon2" android:textColor="@color/colorAccent" android:gravity="center" android:text="59" android:textSize="20sp" /> <TextView android:id="@+id/colon2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="3.0dip" android:layout_marginRight="3.0dip" android:layout_toLeftOf="@+id/seconds_tv" android:text=":" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/seconds_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:gravity="center" android:text="59" android:textColor="@color/colorAccent" android:textSize="20sp" /> </RelativeLayout> </RelativeLayout>
謝謝觀看,小編祝大家生活愉快!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開屏布局
- Android 實(shí)現(xiàn)閃屏頁和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- Android自定義圓形倒計(jì)時(shí)進(jìn)度條
- Android自帶倒計(jì)時(shí)控件Chronometer使用方法詳解
- Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計(jì)時(shí)效果
- Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer)
- Android實(shí)現(xiàn)圓圈倒計(jì)時(shí)
相關(guān)文章
Android使用手勢(shì)監(jiān)聽器GestureDetector遇到的不響應(yīng)問題
這篇文章主要介紹了Android使用手勢(shì)監(jiān)聽器GestureDetector遇到的不響應(yīng)問題,具有很好的參考價(jià)值,對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Android WebView通過動(dòng)態(tài)的修改js去攔截post請(qǐng)求參數(shù)實(shí)例
這篇文章主要介紹了Android WebView通過動(dòng)態(tài)的修改js去攔截post請(qǐng)求參數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)詳解
GestureDetector使用很方便,提供了單擊,雙擊,長(zhǎng)按等操作的處理,但是一般的定義界面都比較復(fù)雜,還用很多需要注意的地方,這篇文章主要給大家介紹了關(guān)于Android如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)的相關(guān)資料,需要的朋友可以參考下2022-01-01
Android中LinearLayout布局的常用屬性總結(jié)
這篇文章主要介紹了Android中LinearLayout布局的常用屬性總結(jié),包括居中、重心、比例等線性布局中的基本設(shè)置,需要的朋友可以參考下2016-04-04
Windows下React Native的Android環(huán)境部署及布局示例
這篇文章主要介紹了Windows下React Native的Android環(huán)境部署及布局示例,這里安卓開發(fā)IDE建議使用Android Studio,且為Windows安裝npm包管理器,需要的朋友可以參考下2016-03-03
FFmpeg Principle學(xué)習(xí)open_output_file打開輸出文件
這篇文章主要為大家介紹了FFmpeg Principle學(xué)習(xí)open_output_file打開輸出文件示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android自定義View實(shí)現(xiàn)簡(jiǎn)易畫板功能
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)簡(jiǎn)易畫板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

