标签:android activity package public 引导页
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,表示应用已经不是不是第一次进入了。这时我们的功能基本实现了。
本文出自 “没有水勒鱼” 博客,请务必保留此出处http://javaqun.blog.51cto.com/10687700/1735378
标签:android activity package public 引导页
原文地址:http://javaqun.blog.51cto.com/10687700/1735378