我正在做一个改变背景颜色的应用程序,但我现在要做的是使用alertDialog,而我的问题是当我更改屏幕时它不保存上次更改,它显示alertDialog再次。我使用的这个,但我不知道如果我做正确的......我使用的,因为的getPreferences我需要让我的活动默认SharedPreferences具有偏好的警报对话框
// SharedPreferences preferences = getPreferences(MODE_PRIVATE);
// int storedPreference = preferences.getInt("storedInt", 0);
// SharedPreferences.Editor editor = preferences.edit();
// editor.putInt("storedInt", storedPreference);
// editor.commit();
new AlertDialog.Builder(this)
.setTitle("Alert Dialog")
.setMessage("Startup Button Visibility:")
.setPositiveButton("Hidden", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(buttonVisible == true)
{
myLayout2.setVisibility(View.INVISIBLE);
}
else
myLayout2.setVisibility(View.VISIBLE);
// Toast.makeText(getApplicationContext(), "Hidden was clicked", Toast.LENGTH_LONG).show();
// // continue with delete
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setNeutralButton("Visible", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
/**
* Showing alert dialog
*/
.show();
你想要什么?为什么你在没有任何改变的情况下首先获得隐藏并重新设置? –
我已经做了一个应用程序,它改变了你按下按钮后的背景颜色,并且在屏幕上点击也可以出现和消失按钮,现在我使用了alertDialog,它将启动模式(可见,隐藏按钮或取消),但我的问题出现后,我已按下例如:隐藏,它的工作原理,但如果我改变方向alertDialog再次来:/ – jaimito
如果你改变你的设备的方向,你再次重新创建你的活动。如果你的变量已经定义在你的变量中,你可以使用变量来说明对话是否以你想要的方式结束并使用'onsaveInstanceState'保存该变量并使用'Bundle'类在'onRestoreInstance'或'onCreate'中恢复它带有你之前定义的值的'Bundle',那么你不会显示对话框。 – zozelfelfo