Java 如何在onCreate完成之前显示内容-Android
我想为我希望制作的应用程序创建一个启动屏幕。我的第一个解决方案是在几秒钟内显示一个新布局,然后像这样显示一个新布局:Java 如何在onCreate完成之前显示内容-Android,java,android,android-layout,android-asynctask,Java,Android,Android Layout,Android Asynctask,我想为我希望制作的应用程序创建一个启动屏幕。我的第一个解决方案是在几秒钟内显示一个新布局,然后像这样显示一个新布局: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logo) android.os.SystemClock.sleep(5000); setContent
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logo)
android.os.SystemClock.sleep(5000);
setContentView(R.layout.activity_main);
}
但问题是,它显示一个白色屏幕5秒钟,然后进入我的主屏幕。我想我可以通过创建一个新的方法或类来解决这个问题,然后从那里更改布局,但是使用一个方法什么都没有做,创建一个类只会变得更加困难,因为我非常确定我必须使用AsyncTask。我觉得有一个非常简单的解决方案我不知道。
提前感谢,,
施穆克先生您需要为启动屏幕创建另一个活动 示例:
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.activity_splash);
new Handler().postDelayed(new Runnable() {
@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, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
这是一个完整的例子
希望它有帮助您需要为启动屏幕创建另一个活动 示例:
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.activity_splash);
new Handler().postDelayed(new Runnable() {
@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, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
这是一个完整的例子
希望它能帮助你不是实现你想要的目标的正确方法…至少在发布前搜索有这么多关于此的线程可能重复的不是实现你想要的目标的正确方法…至少在发布前搜索有这么多关于此的线程可能重复的感谢你的快速回复回复我尝试了一下,但它在“newhandler()”中给了我一个错误。它说它是抽象的,无法实例化。谢谢你的快速回复!我尝试了一下,但它在“newhandler()”中给了我一个错误。它说它是抽象的,无法实例化。