在我的应用程序中,我在android中创建了一个启动画面类型的东西。它应该保持5秒钟。我的问题是我如何在5秒后自动显示另一个活动?启动画面没有按钮,而是在5秒钟后自动显示另一个活动而不点击按钮。请指导我如何设计新的全屏活动。我从堆栈溢出中获取此代码,但由于我是初学者,我不知道在哪里添加此代码,任何人都可以请告诉我。
答案 0 :(得分: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();
}}
然后记得在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>
答案 1 :(得分:0)
您可以在本教程中找到如何创建启动画面:https://www.youtube.com/watch?v=XwOuTjUFexE
答案 2 :(得分: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);
}
答案 3 :(得分: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);
}
}