您好,登錄后才能下訂單哦!
APP引導頁是每個優質APP必備的元素,下面我們研究一下如何只在第一次進入應用時進行加載引導頁的方法。
1.判斷是否第一次進入應用的方法:
package com.yayun.guide; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.Toast; /** * 功能:使用ViewPager實現初次進入應用時的引導頁 * * (1)判斷是否是首次加載應用--采取讀取SharedPreferences的方法 * (2)是,則進入引導activity;否,則進入MainActivity * (3)5s后執行(2)操作 * * @author yayun * */ public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_splash); boolean mFirst = isFirstEnter(SplashActivity.this,SplashActivity.this.getClass().getName()); Toast.makeText(this, mFirst+, Toast.LENGTH_SHORT).show(); if(mFirst) mHandler.sendEmptyMessageDelayed(SWITCH_GUIDACTIVITY,100); else mHandler.sendEmptyMessageDelayed(SWITCH_MAINACTIVITY,100); SharedPreferences sharedPreferences= this.getSharedPreferences(my_pref, MODE_PRIVATE); sharedPreferences.edit().putString(guide_activity, false).commit(); } //**************************************************************** // 判斷應用是否初次加載,讀取SharedPreferences中的guide_activity字段 //**************************************************************** private static final String SHAREDPREFERENCES_NAME = my_pref; private static final String KEY_GUIDE_ACTIVITY = guide_activity; private boolean isFirstEnter(Context context,String className){ if(context==null || className==null||.equalsIgnoreCase(className))return false; String mResultStr = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_WORLD_READABLE) .getString(KEY_GUIDE_ACTIVITY, );//取得所有類名 如 com.my.MainActivity if(mResultStr.equalsIgnoreCase(false)) return false; else return true; } //************************************************* // Handler:跳轉至不同頁面 //************************************************* private final static int SWITCH_MAINACTIVITY = 1000; private final static int SWITCH_GUIDACTIVITY = 1001; public Handler mHandler = new Handler(){ public void handleMessage(Message msg) { switch(msg.what){ case SWITCH_MAINACTIVITY: Intent mIntent = new Intent(); mIntent.setClass(SplashActivity.this, Welcome.class); SplashActivity.this.startActivity(mIntent); SplashActivity.this.finish(); break; case SWITCH_GUIDACTIVITY: mIntent = new Intent(); mIntent.setClass(SplashActivity.this, GuideActivity.class); SplashActivity.this.startActivity(mIntent); SplashActivity.this.finish(); break; } super.handleMessage(msg); } }; }
原理很簡單,進入的時候進行判斷有無存值,若無存值則表示第一次進入,這時跳入應用引導頁,否則,跳入主頁面。
2.ViewPager實現翻頁效果:
package com.yayun.guide; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.Window; import android.widget.Button; import android.widget.LinearLayout; public class GuideActivity extends Activity implements OnClickListener { private ViewPager mViewPager; private PagerAdapter mAdapter; private List<view> mViews = new ArrayList<view>(); // TAB private LinearLayout mTabWeixin; private LinearLayout mTabFrd; private LinearLayout mTabAddress; private LinearLayout mTabSetting; private Button mEnterButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); initEvents(); } private void initEvents() { mViewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } private void initView() { mViewPager = (ViewPager) findViewById(R.id.id_viewpager); LayoutInflater mInflater = LayoutInflater.from(this); View tab01 = mInflater.inflate(R.layout.tab01, null); View tab02 = mInflater.inflate(R.layout.tab02, null); View tab03 = mInflater.inflate(R.layout.tab03, null); View tab04 = mInflater.inflate(R.layout.tab04, null); mViews.add(tab01); mViews.add(tab02); mViews.add(tab03); mViews.add(tab04); mEnterButton=(Button)tab04.findViewById(R.id.imgbtn_enter); mEnterButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(GuideActivity.this,Welcome.class); startActivity(intent); } }); mAdapter = new PagerAdapter() { @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(mViews.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { View view = mViews.get(position); container.addView(view); return view; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public int getCount() { return mViews.size(); } }; mViewPager.setAdapter(mAdapter); } @Override public void onClick(View v) { } } </view></view>
3.引導頁布局文件:
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> </android.support.v4.view.viewpager> </linearlayout>
下面運行一下代碼:
我們可以發現,第一次進入頁面的時候,Toast打印出true,表示應用第一次進入,這時加載引導頁。
退出應用,再一次進入時,Toast打印false,表示應用已經不是不是第一次進入了。這時我們的功能基本實現了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。