2016-09-23 70 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); 
} 

}