日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Android Splash界面支持用户点击 直接进入主界面

發布時間:2023/11/27 生活经验 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Splash界面支持用户点击 直接进入主界面 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載請注明出處:http://blog.csdn.net/lmj623565791/article/details/23613403

現在大部分APP都有Splash界面,下面列一下Splash頁面的幾個作用:

1、展示logo,提高公司形象

2、初始化數據 (拷貝數據到SD)

3、提高用戶體驗?

4、連接服務器是否有新的版本等。

不過如果Splash頁面不做任何操作時,我更喜歡提供個用戶點擊Splash界面直接進入主界面。

一般我們的SplashActivity會這么寫:

package com.example.testsplashdemo;import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;public class SplashActivity extends Activity
{private Handler handler = new Handler();@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handler.postDelayed(new Runnable(){@Overridepublic void run(){Intent intent = new Intent(SplashActivity.this, MainActivity.class);startActivity(intent);}}, 3000);}}
現在我們添加個用戶觸摸屏幕直接進入主界面:

@Overridepublic boolean onTouchEvent(MotionEvent event){if(event.getAction()==MotionEvent.ACTION_UP){Intent intent = new Intent(SplashActivity.this, MainActivity.class);startActivity(intent);finish();}return super.onTouchEvent(event);}

然后測試,會發現用戶如果直接觸摸進入,當3秒后還是會再進入一次主界面。

于是我們改寫代碼為:

package com.example.testsplashdemo;import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;public class SplashActivity extends Activity
{private Handler handler = new Handler();private Runnable runnable;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handler.postDelayed(runnable = new Runnable(){@Overridepublic void run(){Intent intent = new Intent(SplashActivity.this, MainActivity.class);startActivity(intent);finish();}}, 3000);}@Overridepublic boolean onTouchEvent(MotionEvent event){if(event.getAction()==MotionEvent.ACTION_UP){Intent intent = new Intent(SplashActivity.this, MainActivity.class);startActivity(intent);finish();if (runnable != null)handler.removeCallbacks(runnable);}return super.onTouchEvent(event);}}
定義了個Runnable對象,用戶點擊后移除回調事件,解決了。

利用handler.postDelay和removeCallback還可以判斷用戶是否長按操作等。

可能有些哥們會問,我一般是通過發送消息進入主界面的,有什么好的處理方案嗎?

由于原理類似,我就直接貼代碼了:

package com.example.testsplashdemo;import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;public class SplashActivity extends Activity
{private Handler handler = new Handler(){public void handleMessage(android.os.Message msg){Intent intent = new Intent(SplashActivity.this, MainActivity.class);startActivity(intent);finish();handler.removeMessages(-1);};};private Runnable runnable;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handler.sendMessageDelayed(handler.obtainMessage(-1), 3000);}@Overridepublic boolean onTouchEvent(MotionEvent event){if (event.getAction() == MotionEvent.ACTION_UP){handler.sendMessage(handler.obtainMessage(-1));finish();}return super.onTouchEvent(event);}}
也可以解決。

這里最后順便介紹一種比較另類的處理方式:

private Handler handler = new Handler(){public void handleMessage(android.os.Message msg){Intent intent = new Intent(SplashActivity.this, MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);startActivity(intent);finish();};};

其余代碼和上面的代碼一樣,就去掉了移除消息的部分,添加了一個Intent的flag。當僅有這兩個Activity時是沒有問題的,感覺也可以。但是要明白FLAG_ACTIVITY_SINGLE_TOP的含義是,當此Activity存在且位于棧頂時復用,也就是說,3秒內用戶進入別的Activity了,依然會發生重新進入的現象。





轉載于:https://www.cnblogs.com/oversea201405/p/3752039.html

總結

以上是生活随笔為你收集整理的Android Splash界面支持用户点击 直接进入主界面的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。