我正在制作一个滑块演练,当用户单击该应用程序时,该项就引入了应用程序中的第一项...。​​当用户再次打开它时,它就消失了。

像这样:https://www.youtube.com/watch?v=va2IRW_e7_w

问题是,每当我打开应用程序时,我都会一次又一次地重新获得它,这是一个小错误,我找不到它。

Main2Activity:

    public class Main2Activity extends AppCompatActivity {

        Button next, skip;
        private ViewPagerAdapter adapter;
        private ViewPager viewPager;
        private SliderWalkthrough sliderWalkthrough;
        private int[] layouts;
        private TextView[] dots;
        private LinearLayout dotsLayout;

ViewPager.OnPageChangeListener listener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            addButtonDots(position);
            if (position == layouts.length - 1) {

                next.setText("GET STARTED");
                skip.setVisibility(View.GONE);
            } else {
                next.setText("NEXT");
                skip.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            sliderWalkthrough = new SliderWalkthrough(this);
            if (!sliderWalkthrough.Check()) {
                sliderWalkthrough.setFirst(false);
                Intent intent = new Intent(Main2Activity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            if (Build.VERSION.SDK_INT >= 21) {

                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

            }
            setContentView(R.layout.activity_main2);

            viewPager = (ViewPager) findViewById(R.id.id_WalkThroughViewPager);
            dotsLayout = (LinearLayout) findViewById(R.id.layout_dots);

            layouts = new int[]{R.layout.slider_walkthrough_screen1, R.layout.slider_walkthrough_screen2, R.layout.slider_walkthrough_screen3};
            addButtonDots(0);

            adapter = new ViewPagerAdapter();
            viewPager.setAdapter(adapter);
            viewPager.addOnPageChangeListener(listener);

        }

        private void addButtonDots(int position) {

            dots = new TextView[layouts.length];
            int[] colorActive = getResources().getIntArray(R.array.active_dot);
            int[] colorInactive = getResources().getIntArray(R.array.inactive_dot);
            dotsLayout.removeAllViews();
            for (int i = 0; i < dots.length; i++) {
                dots[i] = new TextView(this);
                dots[i].setTextSize(30);
                dots[i].setTextColor(colorInactive[position]);
                dotsLayout.addView(dots[i]);

            }
            if (dots.length > 0) {
                dots[position].setTextColor(colorActive[position]);
            }

        }
    }


SliderWalkthrough:

public class SliderWalkthrough {

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    Context context;

    public SliderWalkthrough(Context context) {
        this.context = context;
        sharedPreferences=context.getSharedPreferences("first",0);
        editor=sharedPreferences.edit();
     }

    public void setFirst(Boolean firstTime){
        editor.putBoolean("check",firstTime);
        editor.commit();
    }

    public boolean Check()
    {
        return sharedPreferences.getBoolean("check",true);
    }
}

最佳答案

演练完成后,您需要将preference值更改为false,否则将不会执行if (!sliderWalkthrough.Check()) {,并且每次都会显示演练

    @Override
    public void onPageSelected(int position) {

        addButtonDots(position);
        if (position == layouts.length - 1) {
            next.setText("GET STARTED");
            skip.setVisibility(View.GONE);
            sliderWalkthrough.setFirst(false);
            //^^^^^^ add this case
        }
         else {
            next.setText("NEXT");
            skip.setVisibility(View.VISIBLE);
        }
    }


注意:删除sliderWalkthrough.setFirst(false);,不再需要

        if (!sliderWalkthrough.Check()) {
            Intent intent = new Intent(Main2Activity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }

10-08 14:00