-1

我正在做一个启动画面,在一定时间后它会切换到另一个屏幕。如果用户不想等待该时间,他们可以点击初始屏幕以切换到另一个屏幕。但问题是,当用户点击启动屏幕时,它会转到另一个屏幕。同时在一定时间内自动运行,以使第二个屏幕打开两次。如何克服这个问题?

下面是我尝试过的代码,我需要处理点击并自动切换到另一个屏幕,但它失败了。

 public class SplashScreen extends AppCompatActivity {
        private TextView bride, groom, weds;
        private TextView date;
        private LinearLayout animLayout;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    //Remove notification bar
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

            setContentView(R.layout.activity_splash);

         new Handler().postDelayed(new Runnable() {
    // when user taps and move to next screen, after certain duration, this also calls where two times WelcomePage is called.

                @Override
                public void run() {
                    // This method will be executed once the timer is over
                    // Start your app main activity
                    Intent i = new Intent(SplashScreen.this, WelcomePage.class);
                    startActivity(i);
                    //overridePendingTransition(R.drawable.from_middle, R.drawable.to_middle);
                    overridePendingTransition(R.anim.left_right_anim, R.anim.translate);
                    // close this activity
                    finish();
                }
            }, 5000);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            int X = (int) event.getX();
            int Y = (int) event.getY();
            int eventaction = event.getAction();

            switch (eventaction) {
                case MotionEvent.ACTION_DOWN:
  //i need to handle this case too..when user taps the screen to move.


  Intent i = new Intent(SplashScreen.this, WelcomePage.class);
                startActivity(i);
                //overridePendingTransition(R.drawable.from_middle, R.drawable.to_middle);
                overridePendingTransition(R.anim.left_right_anim, R.anim.translate);
                // close this activity
                finish();
                break;
        }
        return true;

    }
4

1 回答 1

0

创建两个变量。

Handler handler;
Runnable runnable;

在您的 onCreate 方法中执行此操作。

handler = new Handler;
runnable = new Runnable() {
// when user taps and move to next screen, after certain duration, this also 
 calls where two times WelcomePage is called.

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen.this, WelcomePage.class);
                startActivity(i);
                //overridePendingTransition(R.drawable.from_middle, 
            R.drawable.to_middle);
                overridePendingTransition(R.anim.left_right_anim, 
             R.anim.translate);
                // close this activity
                finish();
            }
        }

然后将runnable添加到处理程序

handler.postDelayed(runnable,5000);

在触摸事件中

case MotionEvent.ACTION_DOWN: handler.removeCallbacks(runnable);

于 2017-05-07T12:15:32.067 回答