- 首页 > it技术 > >
//引导也布局
//.java
package com.example.yindao;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {private ViewPager vp_splash;
private Button btn_goto_main;
private List imgIds;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initListener();
}private void initData() {
sp = getSharedPreferences("isFirstUse", MODE_PRIVATE);
if(sp.getBoolean("fin", false)){
startActivity(new Intent(MainActivity.this,LoginActivityActivity.class));
finish();
}else{
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean("fin", true);
edit.commit();
}imgIds = new ArrayList<>();
imgIds.add(R.drawable.one);
imgIds.add(R.drawable.two);
imgIds.add(R.drawable.tree);
vp_splash.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return imgIds.size();
}@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView img = new ImageView(MainActivity.this);
img.setImageResource(imgIds.get(position));
container.addView(img);
return img;
}@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
});
}
private void initListener() {
vp_splash.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Override
public void onPageSelected(int position) {
if (position == imgIds.size() - 1) {
btn_goto_main.setVisibility(View.VISIBLE);
} else {
btn_goto_main.setVisibility(View.GONE);
}
}@Override
public void onPageScrollStateChanged(int state) {}
});
}private void initView() {
vp_splash = (ViewPager) findViewById(R.id.vp_splash);
btn_goto_main = (Button) findViewById(R.id.btn_goto_main);
btn_goto_main.setOnClickListener(this);
}@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_goto_main:
Intent intent = new Intent(this, LoginActivityActivity.class);
startActivity(intent);
break;
}
}
}
推荐阅读