SplashScreen.javajava
import java.io.File; import java.io.OutputStream; import com.baite.tools.AssetDatabaseManager; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.graphics.PixelFormat; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.TextView; public class SplashScreen extends Activity { /** * Called when the activity is first created. */ private SharedPreferences preferences; private Editor editor; private OutputStream os; @Override public void onCreate(Bundle icicle) { requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉活動上面的黑色bar super.onCreate(icicle); setContentView(R.layout.activity_splash_screen); preferences = getSharedPreferences("phone", Context.MODE_PRIVATE); //判斷是否是首次登陸, if (preferences.getBoolean("firststart", true)) { //這句沒有符合判斷 System.out.println("首次使用!"); editor = preferences.edit(); //將登陸標誌位設置爲false,下次登陸時不在顯示首次登陸界面 editor.putBoolean("firststart", false); editor.commit(); new Handler().postDelayed(new Runnable() { public void run() { /* Create an Intent that will start the Main WordPress Activity. */ System.out.println("進入引導頁面!"); AssetDatabaseManager.initManager(getApplication()); Intent intent = new Intent(SplashScreen.this,GuidPage.class); startActivity(intent); finish(); } }, 2900); //2900 for release } else{ new Handler().postDelayed(new Runnable() { public void run() { /* Create an Intent that will start the Main WordPress Activity. */ System.out.println("進入引導頁面!"); AssetDatabaseManager.initManager(getApplication()); Intent intent = new Intent(SplashScreen.this,MainActivity.class); startActivity(intent); finish(); } }, 2900); //2900 for release System.out.println("不是首次使用!"); } } }
GuidePage.javaandroid
import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import com.baite.tools.ViewPagerAdapter; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.LinearLayout; public class GuidPage extends Activity implements OnPageChangeListener, OnClickListener { // 定義ViewPager對象 private ViewPager viewPager; // 定義ViewPager適配器 private ViewPagerAdapter vpAdapter; // 定義一個ArrayList來存放View private ArrayList<View> views; private View view; // 引導圖片資源 private static final int[] pics = { R.drawable.guide1, R.drawable.guide2, R.drawable.guide3, R.drawable.guide4 }; // 底部小點的圖片 private ImageView[] points; // 記錄當前選中位置 private int currentIndex; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉活動上面的黑色bar super.onCreate(savedInstanceState); setContentView(R.layout.activity_guid_page); /*Button newLife=(Button)findViewById(R.id.startnewlife); newLife.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(GuidPage.this,MainBord.class); GuidPage.this.startActivity(intent); } });*/ //Button start =(Button)findViewById(R.id.guid2start); /*start.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(GuidPage.this,MainBord.class); GuidPage.this.startActivity(intent); GuidPage.this.finish(); } });*/ initView(); initData(); } public void writeFile(String fileName,String writestr) throws IOException{ //寫文件 try{ FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE); byte [] bytes = writestr.getBytes(); fout.write(bytes); fout.close(); } catch(Exception e){ e.printStackTrace(); } } /** * 初始化組件 */ private void initView() { // 實例化ArrayList對象 views = new ArrayList<View>(); // 實例化ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); // 實例化ViewPager適配器 vpAdapter = new ViewPagerAdapter(views); } /** * 初始化數據 */ private void initData() { // 定義一個佈局並設置參數 LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); // 初始化引導圖片列表 for (int i = 0; i < pics.length-1; i++) { ImageView iv = new ImageView(this); iv.setLayoutParams(mParams); //防止圖片不能填滿屏幕 iv.setScaleType(ScaleType.FIT_XY); //加載圖片資源 iv.setImageResource(pics[i]); views.add(iv); } LayoutInflater inflater = LayoutInflater.from(this); view=inflater.inflate(R.layout.last_page, null); views.add(view); Button newlife= (Button)view.findViewById(R.id.startnewlife); newlife.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(GuidPage.this,MainActivity.class); GuidPage.this.startActivity(intent); try { writeFile("flag.txt", "WXDJGKFI78DKGPOK"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } GuidPage.this.finish(); } }); // 設置數據 viewPager.setAdapter(vpAdapter); // 設置監聽 viewPager.setOnPageChangeListener(this); // 初始化底部小點 initPoint(); } /** * 初始化底部小點 */ private void initPoint() { LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll); points = new ImageView[pics.length]; // 循環取得小點圖片 for (int i = 0; i < pics.length; i++) { // 獲得一個LinearLayout下面的每個子元素 points[i] = (ImageView) linearLayout.getChildAt(i); // 默認都設爲灰色 points[i].setEnabled(true); // 給每一個小點設置監聽 points[i].setOnClickListener(this); // 設置位置tag,方便取出與當前位置對應 points[i].setTag(i); } // 設置當面默認的位置 currentIndex = 0; // 設置爲白色,即選中狀態 points[currentIndex].setEnabled(false); } /** * 滑動狀態改變時調用 */ public void onPageScrollStateChanged(int arg0) { } /** * 當前頁面滑動時調用 */ public void onPageScrolled(int arg0, float arg1, int arg2) { } /** * 新的頁面被選中時調用 */ public void onPageSelected(int arg0) { // 設置底部小點選中狀態 setCurDot(arg0); } public void onClick(View v) { int position = (Integer) v.getTag(); setCurView(position); setCurDot(position); } /** * 設置當前頁面的位置 */ private void setCurView(int position) { if (position < 0 || position >= pics.length) { return; } viewPager.setCurrentItem(position); } /** * 設置當前的小點的位置 */ private void setCurDot(int positon) { if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) { return; } points[positon].setEnabled(false); points[currentIndex].setEnabled(true); currentIndex = positon; } }
ViewPagerAdapter.javaapp
package com.baite.tools; import java.util.ArrayList; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; /** * * @author YeChao * @功能描述:ViewPager適配器,用來綁定數據和view */ public class ViewPagerAdapter extends PagerAdapter{ //界面列表 private ArrayList<View> views; public ViewPagerAdapter(ArrayList<View> views) { this.views = views; } /** * 得到當前界面數 */ @Override public int getCount() { if (views != null) { return views.size(); } else return 0; } /** * 判斷是否由對象生成界面 */ @Override public boolean isViewFromObject(View arg0, Object arg1) { return (arg0 == arg1); } /** * 銷燬position位置的界面 */ @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView(views.get(position)); } /** * 初始化position位置的界面 */ @Override public Object instantiateItem(View container, int position) { ((ViewPager) container).addView(views.get(position), 0); return views.get(position); } }
activity_guid_page.xmlide
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="24.0dip" android:orientation="horizontal" > <ImageView android:listSelector="@drawable/selector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:clickable="true" android:contentDescription="guid" android:padding="15.0dip" android:src="@drawable/selector" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:clickable="true" android:contentDescription="guid" android:padding="15.0dip" android:src="@drawable/selector" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:clickable="true" android:contentDescription="guid" android:padding="15.0dip" android:src="@drawable/selector" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:clickable="true" android:contentDescription="guid" android:padding="15.0dip" android:src="@drawable/selector" /> </LinearLayout> </RelativeLayout>