2017-05-07 46 views
-1

我正在做一個啓動畫面,在這個啓動畫面中,在一段時間後它切換到另一個畫面。如果用戶不想等待該持續時間,他們可以點擊啓動畫面切換到另一個屏幕。但問題是,當用戶點擊啓動畫面時,它會轉到另一個畫面。同時自動運行一定時間也會運行,以便第二個屏幕打開兩次。如何解決這個問題?如何處理自動屏幕切換,並觸摸事件移動到另一個屏幕android?

下面是我嘗試在哪裏需要處理點擊和自動切換到另一個屏幕但代碼失敗的代碼。

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; 

    } 
+0

添加'handler.removecallbacksandmessages(空)'在開關殼體 – Raghunandan

回答

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(); 
      } 
     } 

然後添加可運行到該處理程序

handler.postDelayed(runnable,5000); 

而在觸摸事件

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

相關問題