我正在尝试在我的应用中添加一个欢迎屏幕,该屏幕会在第一次安装时显示。在我的情况下,每次应用程序打开时都会显示欢迎屏幕...
这是我开发的代码,用于在我的应用中启用欢迎屏幕
public class SplashScreen_Activity extends AppCompatActivity {
private ViewPager viewPager;
private LinearLayout layoutDot;
private TextView[] dotstv;
private int[] layouts;
private Button btnNext;
private Button btnSkip;
private MyPagerAdapter myPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isFirstTimeStartApp()) {
startMainActivity();
finish();
}
setStatusbarTransparent();
setContentView(R.layout.activity_splash_screen_);
viewPager = findViewById(R.id.view_pager);
layoutDot =(LinearLayout) findViewById(R.id.dotlayout);
btnNext =(Button) findViewById(R.id.btn_next);
btnSkip =(Button) findViewById(R.id.btn_skip);
btnSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startMainActivity();
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int currentpage = viewPager.getCurrentItem() + 1;
if (currentpage < layouts.length) {
viewPager.setCurrentItem(currentpage);
} else {
startMainActivity();
}
}
});
layouts = new int[]{R.layout.slider_1, R.layout.slider_2, R.layout.slider_3};
myPagerAdapter = new MyPagerAdapter(layouts, getApplicationContext());
viewPager.setAdapter(myPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == layouts.length - 1) {
btnNext.setText("START");
btnSkip.setVisibility(View.GONE);
} else {
btnNext.setText("NEXT");
btnSkip.setVisibility(View.VISIBLE);
}
setDotStatus(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
setDotStatus(0);
}
private boolean isFirstTimeStartApp() {
SharedPreferences ref = getApplicationContext().getSharedPreferences("IndroSliderApp", Context.MODE_PRIVATE);
return ref.getBoolean("Flag", true);
}
private void setFirstTimeStartStatus(boolean stt) {
SharedPreferences ref = getApplicationContext().getSharedPreferences("IndroSliderApp", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = ref.edit();
editor.putBoolean("FirstTimeStartFlag", stt);
editor.commit();
}
private void setDotStatus(int page) {
layoutDot.removeAllViews();
dotstv = new TextView[layouts.length];
for (int i = 0; i < dotstv.length; i++) {
dotstv[i] = new TextView(this);
dotstv[i].setText(Html.fromHtml("•"));
dotstv[i].setTextSize(30);
dotstv[i].setTextColor(Color.parseColor("#a9b4bb"));
layoutDot.addView(dotstv[i]);
}
if (dotstv.length > 0) {
dotstv[page].setTextColor(Color.parseColor("#ffffff"));
}
}
private void startMainActivity() {
setFirstTimeStartStatus(false);
startActivity(new Intent(SplashScreen_Activity.this, MainActivity.class));
finish();
}
private void setStatusbarTransparent() {
if (Build.VERSION.SDK_INT >= 21) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
}
}
我没有遇到任何错误,但每次打开应用程序时都会显示欢迎屏幕。 感谢...
答案 0 :(得分:0)
尝试,在您的onCreate()方法中,在 if 条件
之后添加以下代码 if (!isFirstTimeStartApp()) {
startMainActivity();
finish();
}else{
setFirstTimeStartStatus(false);
}
在setFirstTimeStartStatus()方法中,将代码更改为
private void setFirstTimeStartStatus(boolean stt) {
SharedPreferences ref = getApplicationContext().getSharedPreferences("IndroSliderApp", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = ref.edit();
editor.putBoolean("Flag", stt);
editor.commit();
}