Android ViewPager欢迎页+引导页+进入首页

 1 import android.app.Activity;
 2 import android.content.Intent;
 3 import android.content.SharedPreferences;
 4 import android.content.SharedPreferences.Editor;
 5 import android.os.Bundle;
 6 import android.os.Handler;
 7 
 8 /** 欢迎界面 */
 9 public class WelcomeAct extends Activity {
10 
11     private boolean isFirstIn = false;
12     private static final int TIME = 2000;
13     private static final int GO_HOME = 1000;
14     private static final int GO_GUIDE = 1001;
15 
16     private Handler mHandler = new Handler() {
17         public void handleMessage(android.os.Message msg) {
18             switch (msg.what) {
19             // 跳入主界面
20             case GO_HOME:
21                 goHome();
22                 break;
23             // 跳入引导页
24             case GO_GUIDE:
25                 goGuide();
26                 break;
27             }
28         };
29     };
30 
31     @Override
32     protected void onCreate(Bundle savedInstanceState) {
33         super.onCreate(savedInstanceState);
34         setContentView(R.layout.welcome);
35         init();
36     }
37 
38     private void init() {
39         SharedPreferences perPreferences = getSharedPreferences("jike",
40                 MODE_PRIVATE);
41         isFirstIn = perPreferences.getBoolean("isFirstIn", true);
42         if (!isFirstIn) {
43             mHandler.sendEmptyMessageDelayed(GO_HOME, TIME);
44         } else {
45             mHandler.sendEmptyMessageDelayed(GO_GUIDE, TIME);
46             Editor editor = perPreferences.edit();
47             editor.putBoolean("isFirstIn", false);
48             editor.commit();
49         }
50     }
51 
52     private void goHome() {
53         Intent i = new Intent(WelcomeAct.this, MainActivity.class);
54         startActivity(i);
55         finish();
56     }
57 
58     private void goGuide() {
59         Intent i = new Intent(WelcomeAct.this, Guide.class);
60         startActivity(i);
61         finish();
62     }
63 
64 }

 

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.support.v4.view.ViewPager;
 7 import android.support.v4.view.ViewPager.OnPageChangeListener;
 8 import android.view.LayoutInflater;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 import android.widget.ImageView;
13 
14 /** 引导页 */
15 public class Guide extends Activity implements OnPageChangeListener {
16 
17     private ViewPager vp;
18     private ViewPagerAdapter vpAdapter;
19     private List<View> views;
20     private ImageView[] dots;
21     private int[] ids = { R.id.iv1, R.id.iv2, R.id.iv3 };
22     private Button start_btn;
23 
24     @Override
25     protected void onCreate(Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27         setContentView(R.layout.guide);
28         initViews();
29         initDots();
30     }
31 
32     private void initViews() {
33         LayoutInflater inflater = LayoutInflater.from(this);
34 
35         views = new ArrayList<View>();
36         views.add(inflater.inflate(R.layout.one, null));
37         views.add(inflater.inflate(R.layout.two, null));
38         views.add(inflater.inflate(R.layout.three, null));
39 
40         vpAdapter = new ViewPagerAdapter(views, this);
41         vp = (ViewPager) findViewById(R.id.viewpager);
42         vp.setAdapter(vpAdapter);
43         // 下标从0开始,所以第三个页面是get(2)。
44         start_btn = (Button) views.get(2).findViewById(R.id.start_btn);
45         start_btn.setOnClickListener(new OnClickListener() {
46             @Override
47             public void onClick(View arg0) {
48                 Intent i = new Intent(Guide.this, MainActivity.class);
49                 startActivity(i);
50                 finish();
51             }
52         });
53         vp.setOnPageChangeListener(this);
54     }
55 
56     /** 循环设置点 */
57     private void initDots() {
58         dots = new ImageView[views.size()];
59         for (int i = 0; i < views.size(); i++) {
60             dots[i] = (ImageView) findViewById(ids[i]);
61         }
62     }
63 
64     @Override  /** 滑动状态改变的时候 */
65     public void onPageScrollStateChanged(int arg0) {
66         // TODO Auto-generated method stub
67     }
68 
69     @Override  /** 当页面被滑动时候调用 */
70     public void onPageScrolled(int arg0, float arg1, int arg2) {
71         // TODO Auto-generated method stub
72     }
73 
74     @Override  /** 当前新的页面被选中时调用 */
75     public void onPageSelected(int arg0) {
76         for (int i = 0; i < ids.length; i++) {
77             if (arg0 == i) {
78                 // 亮点
79                 dots[i].setImageResource(R.drawable.login_point_selected);
80             } else {
81                 // 暗点
82                 dots[i].setImageResource(R.drawable.login_point);
83             }
84         }
85     }
86 
87 }
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <android.support.v4.view.ViewPager
 7         android:id="@+id/viewpager"
 8         android:layout_width="fill_parent"
 9         android:layout_height="fill_parent"
10         android:background="#00000000" >
11     </android.support.v4.view.ViewPager>
12 
13     <!-- 底部三个点 -->
14     <LinearLayout
15         android:id="@+id/ll"
16         android:layout_width="fill_parent"
17         android:layout_height="wrap_content"
18         android:layout_alignParentBottom="true"
19         android:gravity="center_horizontal"
20         android:orientation="horizontal" >
21         <!-- 选中点 -->
22         <ImageView
23             android:id="@+id/iv1"
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content"
26             android:src="@drawable/login_point_selected" />
27         <!-- 未选中点 -->
28         <ImageView
29             android:id="@+id/iv2"
30             android:layout_width="wrap_content"
31             android:layout_height="wrap_content"
32             android:src="@drawable/login_point" />
33         <!-- 未选中点 -->
34         <ImageView
35             android:id="@+id/iv3"
36             android:layout_width="wrap_content"
37             android:layout_height="wrap_content"
38             android:src="@drawable/login_point" />
39     </LinearLayout>
40 
41 </RelativeLayout>

 

DEMO下载:http://pan.baidu.com/s/1i3vHoLv

posted on 2014-09-26 20:23  大米稀饭  阅读(1436)  评论(0编辑  收藏  举报