国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 實現閃屏頁和右上角的倒計時跳轉實例代碼

2020-04-11 10:45:58
字體:
來源:轉載
供稿:網友

以前編程的時候,遇到倒計時的功能時,經常自己去寫,但其實Android已經幫封裝好了一個倒計時類CountDownTimer,其實是將后臺線程的創建和Handler隊列封裝成為了一個方便的類調用。

閃屏頁用到了handler和CountDownTimer類,還需配置一下Activity的主題,這里是:android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 全屏主題的意思。

給大家展示下效果圖:

代碼如下所示:

package com.example.shanping;import java.lang.ref.WeakReference;import com.example.shanping.MyActivity.MyCountDownTimer;import android.os.Bundle;import android.os.CountDownTimer;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.content.Intent;import android.util.Log;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {private MyCountDownTimer mc; private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv = (TextView) findViewById(R.id.textView1); mc = new MyCountDownTimer(3000, 1000); mc.start();handler.postDelayed(new Runnable() {@Overridepublic void run() {Intent intent=new Intent(MainActivity.this,MyActivity.class);startActivity(intent);}}, 3000);}private Handler handler=new Handler();/** * 繼承 CountDownTimer 防范 * * 重寫 父類的方法 onTick() 、 onFinish() */class MyCountDownTimer extends CountDownTimer { /** * * @param millisInFuture * 表示以毫秒為單位 倒計時的總數 * * 例如 millisInFuture=1000 表示1秒 * * @param countDownInterval * 表示 間隔 多少微秒 調用一次 onTick 方法 * * 例如: countDownInterval =1000 ; 表示每1000毫秒調用一次onTick() * */public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } public void onFinish() { tv.setText("正在跳轉"); } public void onTick(long millisUntilFinished) { tv.setText("倒計時(" + millisUntilFinished / 1000 + ")"); } }}

下面給大家分享一段代碼關于Android實現啟動閃屏界面效果

閃屏,就是SplashScreen,也可以說是啟動畫面,就是啟動的時候,閃(展示)一下,持續數秒后,自動關閉。

android的實現非常簡單,使用Handler對象的postDelayed方法就可以實現。在這個方法里傳遞一個Runnable對象和一個延遲的時間。該方法實現了一個延遲執行的效果,延遲的時間由第2個參數指定,單位是毫秒。第一個參數是Runnable對象,里面包含了延遲后需要執行的操作。demo代碼如下:

java code:

package com.mstar;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;public class ActSplashScreen extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.shan);// 閃屏的核心代碼new Handler().postDelayed(new Runnable() {@Overridepublic void run() {Intent intent = new Intent(ActSplashScreen.this,DialogTest.class); //從啟動動畫ui跳轉到主uistartActivity(intent);ActSplashScreen.this.finish(); // 結束啟動動畫界面}}, 3000); //啟動動畫持續3秒鐘}}

xml code:

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:text="閃一下"></TextView></LinearLayout>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴山县| 惠来县| 随州市| 杨浦区| 枞阳县| 卢龙县| 湖口县| 区。| 定远县| 梁山县| 迭部县| 阿克苏市| 抚远县| 句容市| 翁牛特旗| 诸暨市| 腾冲县| 昆明市| 奉节县| 古田县| 邯郸市| 安泽县| 潮州市| 汶川县| 米林县| 天祝| 娱乐| 南城县| 靖安县| 张家口市| 江安县| 丹江口市| 永安市| 花莲市| 三原县| 泗阳县| 吴江市| 新宁县| 富顺县| 梁平县| 梁平县|