package com.example.zhangjian20170712sharedpreferences02;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class MainActivity extends Activity {
private ViewPager viewPager;
private Button button;
private List<Integer> list;
private SharedPreferences sharedPreferences;
private Editor edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) findViewById(R.id.pager);
button=(Button) findViewById(R.id.btn);
//获取对象
sharedPreferences=getSharedPreferences("config", MODE_PRIVATE);
//去文件判断是否进来信息
boolean flag = sharedPreferences.getBoolean("flag", false);
if (flag) {
enterDetails();
return;
}
list=new ArrayList<Integer>();
list.add(R.drawable.a);
list.add(R.drawable.b);
list.add(R.drawable.c);
list.add(R.drawable.d);
list.add(R.drawable.e);
viewPager.setAdapter(new MyPagerAdapter());
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int position) {
if (position==list.size()-1) {
button.setVisibility(View.VISIBLE);
}else {
button.setVisibility(View.GONE);
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
});
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//拿到文件存储信息
edit = sharedPreferences.edit();
//进入
edit.putBoolean("flag", true);
edit.commit();
enterDetails();
}
});
}
public void enterDetails() {
// TODO Auto-generated method stub
Intent it=new Intent(MainActivity.this,MyActivity.class);
startActivity(it);
//杀死之前的页面
finish();
}
class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(MainActivity.this);
imageView.setImageResource(list.get(position));
imageView.setScaleType(ScaleType.FIT_XY);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View) object);
}
}
}
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class MainActivity extends Activity {
private ViewPager viewPager;
private Button button;
private List<Integer> list;
private SharedPreferences sharedPreferences;
private Editor edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) findViewById(R.id.pager);
button=(Button) findViewById(R.id.btn);
//获取对象
sharedPreferences=getSharedPreferences("config", MODE_PRIVATE);
//去文件判断是否进来信息
boolean flag = sharedPreferences.getBoolean("flag", false);
if (flag) {
enterDetails();
return;
}
list=new ArrayList<Integer>();
list.add(R.drawable.a);
list.add(R.drawable.b);
list.add(R.drawable.c);
list.add(R.drawable.d);
list.add(R.drawable.e);
viewPager.setAdapter(new MyPagerAdapter());
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int position) {
if (position==list.size()-1) {
button.setVisibility(View.VISIBLE);
}else {
button.setVisibility(View.GONE);
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
});
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//拿到文件存储信息
edit = sharedPreferences.edit();
//进入
edit.putBoolean("flag", true);
edit.commit();
enterDetails();
}
});
}
public void enterDetails() {
// TODO Auto-generated method stub
Intent it=new Intent(MainActivity.this,MyActivity.class);
startActivity(it);
//杀死之前的页面
finish();
}
class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(MainActivity.this);
imageView.setImageResource(list.get(position));
imageView.setScaleType(ScaleType.FIT_XY);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View) object);
}
}
}