Android開發(fā)歡迎頁點擊跳過倒計時進入主頁
沒點擊跳過自然進入主頁,點擊跳過之后立即進入主頁
1.歡迎頁布局activity_sp.xml放一張背景圖(圖片隨你便啦)再放一個盛放倒計時的TextView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/a"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:textSize="20sp" /> </RelativeLayout> </LinearLayout>
2.主要java代碼
package com.cwj.test; import android.content.Intent; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.WindowManager; import android.widget.TextView; import java.util.Timer; import java.util.TimerTask; public class SpActivity extends AppCompatActivity implements View.OnClickListener { private int recLen = 5;//跳過倒計時提示5秒 private TextView tv; Timer timer = new Timer(); private Handler handler; private Runnable runnable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //定義全屏參數(shù) int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN; //設(shè)置當前窗體為全屏顯示 getWindow().setFlags(flag, flag); setContentView(R.layout.activity_sp); initView(); timer.schedule(task, 1000, 1000);//等待時間一秒,停頓時間一秒 /** * 正常情況下不點擊跳過 */ handler = new Handler(); handler.postDelayed(runnable = new Runnable() { @Override public void run() { //從閃屏界面跳轉(zhuǎn)到首界面 Intent intent = new Intent(SpActivity.this, MainActivity.class); startActivity(intent); finish(); } }, 5000);//延遲5S后發(fā)送handler信息 } private void initView() { tv = findViewById(R.id.tv);//跳過 tv.setOnClickListener(this);//跳過監(jiān)聽 } TimerTask task = new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { // UI thread @Override public void run() { recLen--; tv.setText("跳過 " + recLen); if (recLen < 0) { timer.cancel(); tv.setVisibility(View.GONE);//倒計時到0隱藏字體 } } }); } }; /** * 點擊跳過 */ @Override public void onClick(View view) { switch (view.getId()) { case R.id.tv: //從閃屏界面跳轉(zhuǎn)到首界面 Intent intent = new Intent(SpActivity.this, MainActivity.class); startActivity(intent); finish(); if (runnable != null) { handler.removeCallbacks(runnable); } break; default: break; } } }
這里style里改了樣式,沒有標題欄Theme.AppCompat.Light.NoActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>
補充:
Android程序跳過登錄界面直接進入主界面(自動登錄)
首先是歡迎界面的代碼
public class WelcomeActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome); /** * 延遲3秒進入主界面 */ new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent=new Intent(WelcomeActivity.this,LoginActivity.class); startActivity(intent); WelcomeActivity.this.finish(); } },1000*3); } }
接下來是文章的主要內(nèi)容。實現(xiàn)自動登錄的關(guān)鍵是當程序從歡迎界面跳轉(zhuǎn)到登錄界面是,在登錄界面還沒有加載布局文件時判斷是否登陸過,從而實現(xiàn)直接跳轉(zhuǎn)到主界面。這里我們采用SharedPreferences來保存登錄狀態(tài)。代碼如下:
public class LoginActivity extends Activity{ SharedPreferences sprfMain; SharedPreferences.Editor editorMain; Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //在加載布局文件前判斷是否登陸過 sprfMain= PreferenceManager.getDefaultSharedPreferences(this); editorMain=sprfMain.edit(); //.getBoolean("main",false);當找不到"main"所對應(yīng)的鍵值是默認返回false if(sprfMain.getBoolean("main",false)){ Intent intent=new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); LoginActivity.this.finish(); } setContentView(R.layout.login); login= (Button) findViewById(R.id.login); //這里只是簡單用按鍵模擬登錄功能 login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(LoginActivity.this,MainActivity.class); editorMain.putBoolean("main",true); editorMain.commit(); startActivity(intent); LoginActivity.this.finish(); } }); } }
接下來是實現(xiàn)注銷后要重新進入登錄界面
public class MainActivity extends AppCompatActivity { SharedPreferences sprfMain; SharedPreferences.Editor editorMain; Button exit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); exit= (Button) findViewById(R.id.exit); exit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //點擊注銷按鍵后調(diào)用LoginActivity提供的resetSprfMain()方法執(zhí)行editorMain.putBoolean("main",false);,即將"main"對應(yīng)的值修改為false resetSprfMain(); Intent intent=new Intent(MainActivity.this,WelcomeActivity.class); startActivity(intent); MainActivity.this.finish(); } }); } public void resetSprfMain(){ sprfMain= PreferenceManager.getDefaultSharedPreferences(this); editorMain=sprfMain.edit(); editorMain.putBoolean("main",false); editorMain.commit(); } }
到此這篇關(guān)于Android開發(fā)歡迎頁點擊跳過倒計時進入主頁的文章就介紹到這了,更多相關(guān)Android點擊跳過倒計時內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決AndroidStudio無法運行java中的mian方法問題
這篇文章主要介紹了解決AndroidStudio無法運行java中的mian方法問題,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10android中ListView多次刷新重復(fù)執(zhí)行g(shù)etView的解決方法
以前倒是沒有注意listview的getView會重復(fù)執(zhí)行多次,在測試的時候去斷點跟蹤,發(fā)現(xiàn)同一條數(shù)據(jù)不斷的重復(fù)執(zhí)行,下面與大家分享下正確的解決方法,希望對你有所幫助2013-06-06Android利用屬性動畫實現(xiàn)優(yōu)酷菜單
這篇文章主要為大家詳細介紹了Android利用屬性動畫實現(xiàn)優(yōu)酷菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Android 根據(jù)手勢頂部View自動展示與隱藏效果
這篇文章主要介紹了Android 根據(jù)手勢頂部View自動展示與隱藏效果,本文給大家介紹非常詳細包括實現(xiàn)原理和實例代碼,需要的朋友參考下吧2017-08-08android中實現(xiàn)OkHttp下載文件并帶進度條
本篇文章主要介紹了android中實現(xiàn)OkHttp下載文件并帶進度條,OkHttp是比較火的網(wǎng)絡(luò)框架,它支持同步與異步請求,支持緩存,可以攔截,更方便下載大文件與上傳文件的操作,有興趣的可以了解一下2017-07-07Android仿知乎客戶端關(guān)注和取消關(guān)注的按鈕點擊特效實現(xiàn)思路詳解
這篇文章主要介紹了Android仿知乎客戶端關(guān)注和取消關(guān)注的按鈕點擊特效實現(xiàn)思路詳解的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09Android側(cè)滑菜單之DrawerLayout用法詳解
今天小編就為大家分享一篇關(guān)于Android側(cè)滑菜單之DrawerLayout用法詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03