ViewPager控件实现欢迎页

要注意的地方:

1、需要把“是否第一次启动App”,“是否已登录”这些信息写入 SharedPreferences 中去

2、欢迎页如果没有跳转完毕,不能写入“第一次启动App”的信息

3、当页面跳转完毕之后,需要将上一页面 finish();否则有其他情况。

 

上代码:

  1 package com.owen.welcomeviewpage;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 import com.owen.welcomeviewpage.util.SharedPrefUtil;
  7 
  8 import android.app.Activity;
  9 import android.content.Intent;
 10 import android.os.Bundle;
 11 import android.support.v4.view.PagerAdapter;
 12 import android.support.v4.view.ViewPager;
 13 import android.view.View;
 14 import android.view.ViewGroup;
 15 import android.view.Window;
 16 import android.widget.ImageView;
 17 import android.widget.ImageView.ScaleType;
 18 
 19 public class MainActivity extends Activity {
 20     /** 引导页面的控件 */
 21     private ViewPager mViewPager;
 22     /** 三张引导页面的图片 */
 23     private int[] mImgIds = new int[] { R.drawable.viewpage_1,
 24             R.drawable.viewpage_2, R.drawable.viewpage_3 };
 25     /** 图片资源容器 */
 26     private List<ImageView> mImageViews = new ArrayList<ImageView>();
 27 
 28     @Override
 29     protected void onCreate(Bundle savedInstanceState) {
 30         super.onCreate(savedInstanceState);
 31         requestWindowFeature(Window.FEATURE_NO_TITLE);
 32         setContentView(R.layout.activity_main);
 33 
 34         initImgData();
 35         
 36         if (isFirstLaunchApp()) {
 37             initViewPager();
 38         } else if (isLogined()) {
 39             jumpToIndexActivity();
 40         } else {
 41             jumpToLoginActivity();
 42         }
 43     }
 44 
 45     private void initImgData() {
 46         for (int imgId : mImgIds) {
 47             ImageView imageView = new ImageView(getApplicationContext());
 48             imageView.setScaleType(ScaleType.CENTER_CROP);
 49             imageView.setImageResource(imgId);
 50             
 51             mImageViews.add(imageView);
 52         }
 53     }
 54 
 55     /**
 56      * 判断是否是第一次启动App
 57      * 
 58      * @return 是-true 否-false
 59      */
 60     private boolean isFirstLaunchApp() {
 61         return SharedPrefUtil.getIsFirstLauchApp(this);
 62     }
 63     
 64     /**
 65      * 是否已登录
 66      * 
 67      * @return 是-true 否-false
 68      */
 69     private boolean isLogined() {
 70         return SharedPrefUtil.getIsLogined(this);
 71     }
 72     
 73     /**
 74      * 跳转到App主界面
 75      */
 76     private void jumpToIndexActivity() {
 77         Intent intent = new Intent(this, IndexActivity.class);
 78         startActivity(intent);
 79         
 80         finish();
 81     }
 82     
 83     /**
 84      * 跳转到登录页面
 85      */
 86     private void jumpToLoginActivity() {
 87         Intent intent = new Intent(MainActivity.this, WelcomeActivity.class);
 88         startActivity(intent);
 89         
 90         finish();
 91     }
 92     
 93     private void initViewPager() {
 94         mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
 95 
 96         mViewPager.setAdapter(new PagerAdapter() {
 97 
 98             @Override
 99             public Object instantiateItem(ViewGroup container, int position) {
100                 // 给最后一页引导页添加点击事件
101                 if (position == mViewPager.getAdapter().getCount() - 1) {
102                     mImageViews.get(position).setOnClickListener(new View.OnClickListener() {
103                         
104                         @Override
105                         public void onClick(View v) {
106                             //  跳转到登录页面
107                             jumpToLoginActivity();
108                             SharedPrefUtil.writeIsFirstLauchApp(MainActivity.this, false);
109                         }
110                     });
111                 }
112                 
113                 container.addView(mImageViews.get(position));
114                 return mImageViews.get(position);
115             }
116 
117             @Override
118             public void destroyItem(ViewGroup container, int position,
119                     Object object) {
120                 container.removeView(mImageViews.get(position));
121             }
122 
123             @Override
124             public boolean isViewFromObject(View view, Object object) {
125                 return view == object;
126             }
127 
128             @Override
129             public int getCount() {
130                 return mImgIds.length;
131             }
132         });
133     }
134 
135 }

 

这里需要用到工具类 SharePrefUtil.java,用来操作 SharedPreferences 文件

 1 package com.owen.welcomeviewpage.util;
 2 
 3 import android.content.Context;
 4 import android.content.SharedPreferences;
 5 import android.content.SharedPreferences.Editor;
 6 
 7 import com.owen.welcomeviewpage.data.Constant;
 8 
 9 /**
10  * 操作SharedPreferences的工具类
11  * 
12  * @author owen
13  */
14 public class SharedPrefUtil {
15     
16     /**
17      * 读取:是否第一次启动App,默认值为true
18      */
19     public static boolean getIsFirstLauchApp(Context context) {
20         return getBooleanValue(context, Constant.SHARE_PREF_KEY_IS_FIRST_LAUNCH_APP, true);
21     }
22 
23     /**
24      * 写入:是否第一次启动App
25      */
26     public static void writeIsFirstLauchApp(Context context, boolean state) {
27         writeBooleanValue(context, Constant.SHARE_PREF_KEY_IS_FIRST_LAUNCH_APP, state);
28     }
29     
30     /**
31      * 读取:是否已登录,默认值为false
32      */
33     public static boolean getIsLogined(Context context) {
34         return getBooleanValue(context, Constant.SHARE_PREF_KEY_IS_LOGINED, false);
35     }
36 
37     /**
38      * 写入:是否已登录
39      */
40     public static void writeIsLogined(Context context, boolean state) {
41         writeBooleanValue(context, Constant.SHARE_PREF_KEY_IS_LOGINED, state);
42     }
43     
44     /**
45      * 获取 SharedPreferences 对象
46      *
47      * @param context 上下文Context
48      */
49     private static SharedPreferences getSharedPreferences(Context context) {
50         SharedPreferences sharedPref = context.getSharedPreferences(
51                 Constant.SHARE_PREF_FILE_NAME, Context.MODE_PRIVATE);
52         return sharedPref;
53     }
54 
55     /**
56      * 从 SharedPreferences 读取布尔值
57      * 
58      * @param context 上下文Context
59      * @param sharePreKeyName The name of the preference to retrieve.
60      * @param defaultValue Value to return if this preference does not exist.
61      */
62     private static boolean getBooleanValue(Context context, String sharePreKeyName, boolean defaultValue) {
63         SharedPreferences shrePref = getSharedPreferences(context);
64         return shrePref.getBoolean(sharePreKeyName, defaultValue);
65     }
66     
67     /**
68      * 写入布尔值到 SharedPreferences 中
69      * 
70      * @param context 上下文Context
71      * @param sharePreKeyName SharedPreferences 中的键名称
72      * @param valueToWrite 需要写入的布尔值
73      */
74     private static void writeBooleanValue(Context context, String sharePreKeyName, boolean valueToWrite) {
75         SharedPreferences shrePref = getSharedPreferences(context);
76         Editor editor = shrePref.edit();
77         editor.putBoolean(sharePreKeyName, valueToWrite);
78         editor.commit();
79     }
80     
81 }

 

同时,需要规定一下 SharedPreferences 中的键-值对,这里我写了一个常量类 Constant.java

 1 package com.owen.welcomeviewpage.data;
 2 
 3 public class Constant {
 4     
 5     // -------------------------- SharedPreferences ----------------------------
 6     /** SharedPreferences的文件名 */
 7     public static final String SHARE_PREF_FILE_NAME = "com.owen.welcomeviewpager.share_pref_file";
 8     /** 是否第一次启动App */
 9     public static final String SHARE_PREF_KEY_IS_FIRST_LAUNCH_APP = "is_first_launch";
10     /** 是否已登录 */
11     public static final String SHARE_PREF_KEY_IS_LOGINED = "is_logined";
12     
13 }

 

转载于:https://www.cnblogs.com/stayfoolishstayhungry/p/4410027.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值