在很多应用中都有引导页,这几乎是每个应用必不可少的,今天就以百度团购的引导页做例子,
在那就不做多余的解释了,直接上代码,新建一个android项目,guaid,
WelComeActivity.java
package com.handcool.guaid;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
public class WelComeActivity extends Activity {
private SharedPreferences sp;
private Editor editor;
private Intent mIntent;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_welcome);
super.onCreate(savedInstanceState);
sp = getSharedPreferences("user", Context.MODE_PRIVATE);
editor = sp.edit();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
boolean isFirst = sp.getBoolean("isFirst", false);
if(isFirst){
mIntent = new Intent(WelComeActivity.this,MainActivity.class);
startActivity(mIntent);
finish();
}else{
mIntent = new Intent(WelComeActivity.this,GuaidActivity.class);
startActivity(mIntent);
editor.putBoolean("isFirst", true);
editor.commit();
finish();
}
}
}, 3000);
}
}
activity_welcome.xml文件
<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:background="@drawable/splash_bg_newyear"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/splash_horse_newyear"
android:layout_centerInParent="true"
/>
</RelativeLayout>
当应用是第一次进入的时候,进入到引导页
GuaidActivity.java
package com.handcool.guaid;
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.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class GuaidActivity extends Activity {
protected static final String TAG = "GuaidActivity";
private ViewPager viewpager;
private List<ImageView> imgs;
private GuaidAdapter adapter;
private Button btn_skip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guaid);
viewpager = (ViewPager) findViewById(R.id.viewpager);
imgs = new ArrayList<ImageView>();
ImageView img1 = new ImageView(this);
ImageView img2 = new ImageView(this);
ImageView img3 = new ImageView(this);
btn_skip = (Button) findViewById(R.id.btn_skip);
img1.setBackgroundResource(R.drawable.guide_1);
img2.setBackgroundResource(R.drawable.guide_2);
img3.setBackgroundResource(R.drawable.guide_3);
imgs.add(img1);
imgs.add(img2);
imgs.add(img3);
adapter = new GuaidAdapter(imgs);
viewpager.setAdapter(adapter);
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
if(arg0==2){
btn_skip.setVisibility(View.VISIBLE);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
btn_skip.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(GuaidActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
activity_guaid.xml布局文件
非首次进入的话,就直接进入到程序主界面,不过这个主界面啥也没做
MainActivity.java
package com.handcool.guaid;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<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:background="#ffffff"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="主界面"
android:gravity="center"
android:textColor="#ff0000"
android:textSize="20sp"
/>
</RelativeLayout>