2016-09-23 65 views
-4

在我的應用程序中,我創建了一個閃屏類型的東西在android中。它應該保持5秒鐘。我的問題是如何在5秒後自動顯示其他活動?啓動畫面沒有按鈕,相反它應該在5秒後自動顯示另一個活動,而不需要點擊按鈕。並且請指導我如何設計新的全屏活動。我從堆棧溢出得到了這段代碼,但由於我是初學者,我不知道在哪裏添加這段代碼,任何人都可以告訴我。全屏活動設計和自動啓動新活動

+1

不如海在谷歌''如何處理Splash屏幕在Android?' –

+2

可能重複的[如何設置我的啓動畫面的時間限制?](http://stackoverflow.com/questions/19491073/how-do-i -set-A-時限到我,閃屏) –

回答

0

開機畫面加載添加下面的代碼之後在上創建

try { 
       Thread.sleep(5000);// You can change this depending on the requirement 
       Intent intent = new Intent(SplashActivity.this,SecondActivity.class); 
       startActivity(intent); 
       finish(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

你的第二個活動將在5秒鐘後自動被加載。

爲了使活動全屏的setContentView前添加以下線:

getWindow()setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

@Override 
protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.your_activity_layout); 
} 
1

因此可以說你已經爲你的閃屏創建了一個佈局。 然後你需要爲你的啓動畫面創建一個活動。

public class SplashScreen extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); //The layout for this activity 

    Thread timerThread = new Thread(){ 

     public void run(){ 

      try { 

       sleep(5000); //After 5 seconds your next activity will be displayed 

      } catch(InterruptedException e){ 

       e.printStackTrace(); 

      } finally { 

       Intent intent = new Intent(getBaseContext, MainActivity.class); // The next activity you want to start 
       startActivity(intent); 
      } 
     } 
    }; 

    timerThread.start(); 
} 

@Override 
protected void onPause() { 

    super.onPause(); 
    finish(); 
}} 

然後remeber編輯您的活動類別在AndroidManifest.xml文件的啓動畫面類別應該是.LAUNCHER,而你的主要活動應.DEFAULT

 <activity 
     android:name=".SplashScreen" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.example.MAINACTIVITY" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
0

使用閃屏驗證碼活動更改時間如你所願......

public class SplashScreen extends Activity { 

// Splash screen timer 
private static int SPLASH_TIME_OUT = 3000; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 

    new Handler().postDelayed(new Runnable() { 

    /* 
    * Showing splash screen with a timer. This will be useful when you 
    * want to show case your app logo/company 
    */ 

     @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, Login_Activity.class); 
      startActivity(i); 
      //overridePendingTransition(R.anim.fadein,R.anim.fadeout); 

      // close this activity 
      finish(); 
     } 
    }, SPLASH_TIME_OUT); 
} 

}