Android仿活動(dòng)時(shí)分秒倒計(jì)時(shí)效果
本文實(shí)例為大家分享了Android時(shí)分秒倒計(jì)時(shí)效果的具體代碼,供大家參考,具體內(nèi)容如下
從mian.xml下手:
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="離結(jié)束時(shí)間為:"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tvtime1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:background="#3B3B3B"
android:text="12"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<TextView
android:id="@+id/tvtime2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="12"
android:textColor="#FFFFFF"
android:background="#3B3B3B"
android:textSize="30sp" />
<TextView
android:id="@+id/tvtime3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:gravity="center"
android:background="#3B3B3B"
android:textColor="#FFFFFF"
android:text="13"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
private TextView tvtime1,tvtime2,tvtime3;
private long time=400;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvtime1=(TextView)findViewById(R.id.tvtime1);
tvtime2=(TextView) findViewById(R.id.tvtime2);
tvtime3=(TextView) findViewById(R.id.tvtime3);
handler.postDelayed(runnable, 1000);
}
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
time--;
String formatLongToTimeStr = formatLongToTimeStr(time);
String[] split = formatLongToTimeStr.split(":");
for (int i = 0; i < split.length; i++) {
if(i==0){
tvtime1.setText(split[0]+"小時(shí)");
}
if(i==1){
tvtime2.setText(split[1]+"分鐘");
}
if(i==2){
tvtime3.setText(split[2]+"秒");
}
}
if(time>0){
handler.postDelayed(this, 1000);
}
}
};
public String formatLongToTimeStr(Long l) {
int hour = 0;
int minute = 0;
int second = 0;
second = l.intValue() ;
if (second > 60) {
minute = second / 60; //取整
second = second % 60; //取余
}
if (minute > 60) {
hour = minute / 60;
minute = minute % 60;
}
String strtime = hour+":"+minute+":"+second;
return strtime;
}
}
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義DigitalClock控件實(shí)現(xiàn)商品倒計(jì)時(shí)
- Android計(jì)時(shí)與倒計(jì)時(shí)實(shí)現(xiàn)限時(shí)搶購(gòu)的5種方法
- Android定時(shí)器和倒計(jì)時(shí)實(shí)現(xiàn)淘寶秒殺功能
- Android CountDownTimer實(shí)現(xiàn)定時(shí)器和倒計(jì)時(shí)效果
- android自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開(kāi)屏布局
- Android 實(shí)現(xiàn)閃屏頁(yè)和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- Android實(shí)現(xiàn)精確到天時(shí)分秒的搶購(gòu)倒計(jì)時(shí)
相關(guān)文章
android使用NotificationListenerService監(jiān)聽(tīng)通知欄消息
本篇文章主要介紹了android使用NotificationListenerService監(jiān)聽(tīng)通知欄消息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
Android中實(shí)現(xiàn)在矩形框中輸入文字顯示剩余字?jǐn)?shù)的功能
在矩形輸入框框中輸入文字顯示剩余字?jǐn)?shù)的功能在app開(kāi)發(fā)中經(jīng)常會(huì)見(jiàn)到,今天小編就通過(guò)實(shí)例代碼給大家分享android實(shí)現(xiàn)輸入框提示剩余字?jǐn)?shù)功能,代碼簡(jiǎn)單易懂,需要的朋友參考下吧2017-04-04
Android?常見(jiàn)獲取設(shè)備標(biāo)識(shí)方法總結(jié)
隨著Android系統(tǒng)版本更新,Google對(duì)用戶隱私保護(hù)增強(qiáng),限制獲取設(shè)備標(biāo)識(shí),文中測(cè)試DeviceID、ANDROID_ID、Serial、MAC地址等方法在不同API級(jí)別的表現(xiàn),感興趣的朋友跟隨小編一起看看吧2024-09-09
Android編程實(shí)現(xiàn)長(zhǎng)按彈出選項(xiàng)框View進(jìn)行操作的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)長(zhǎng)按彈出選項(xiàng)框View進(jìn)行操作的方法,結(jié)合實(shí)例形式分析了Android事件響應(yīng)及彈窗的功能、布局相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Android自定義Camera實(shí)現(xiàn)拍照小功能
這篇文章主要為大家詳細(xì)介紹了Android自定義Camera實(shí)現(xiàn)拍照小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
深入了解Android Okio的超時(shí)機(jī)制
Okio是一個(gè)IO庫(kù),底層基于Java原生的輸入輸出流實(shí)現(xiàn)。但原生的輸入輸出流并沒(méi)有提供超時(shí)的檢測(cè)機(jī)制。而Okio實(shí)現(xiàn)了這個(gè)功能,本文就來(lái)為大家詳細(xì)講講2023-02-02
實(shí)例講解Android中SQLiteDatabase使用方法
這篇文章主要以一個(gè)簡(jiǎn)單的實(shí)例為大家詳細(xì)講解Android中SQLiteDatabase使用方法,感興趣的小伙伴們可以參考一下2016-05-05
Flutter封裝組動(dòng)畫(huà)混合動(dòng)畫(huà)AnimatedGroup示例詳解
這篇文章主要為大家介紹了Flutter封裝組動(dòng)畫(huà)混合動(dòng)畫(huà)AnimatedGroup示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

