我想在用户首次打开应用程序时在启动屏幕后显示一个警告对话框。我使用this link实现了 Splash Screen 。 指导我怎么做?
样式
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background</item>
</style>
background.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimaryDark" />
<item>
<bitmap
android:gravity="center"
android:src="@drawable/splash" />
</item>
SplashActivity.class
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
答案 0 :(得分:2)
您需要以持久方式(例如数据库)或更简单的方式在SharedPreferences中保存初始化值。启动时检查此值。喜欢:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
boolean isInitialized = sharedPref.getBoolean("INIT_STATE", false);
if (!isInitialized) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Prompt message")
.setMessage("Your message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("INIT_STATE", true);
editor.apply();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
} else {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
当然,此代码是指南,您应该添加错误检查并尽可能避免重复代码等。