2014-01-06 25 views
0

我正在做一个改变背景颜色的应用程序,但我现在要做的是使用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(); 
+0

你想要什么?为什么你在没有任何改变的情况下首先获得隐藏并重新设置? –

+0

我已经做了一个应用程序,它改变了你按下按钮后的背景颜色,并且在屏幕上点击也可以出现和消失按钮,现在我使用了alertDialog,它将启动模式(可见,隐藏按钮或取消),但我的问题出现后,我已按下例如:隐藏,它的工作原理,但如果我改变方向alertDialog再次来:/ – jaimito

+0

如果你改变你的设备的方向,你再次重新创建你的活动。如果你的变量已经定义在你的变量中,你可以使用变量来说明对话是否以你想要的方式结束并使用'onsaveInstanceState'保存该变量并使用'Bundle'类在'onRestoreInstance'或'onCreate'中恢复它带有你之前定义的值的'Bundle',那么你不会显示对话框。 – zozelfelfo

回答

0

根据对你的问题的评论。看起来你想保留你的活动状态,即使你改变了它的方向。将此添加到活动括号内的清单中。

android:configChanges="orientation|screenSize" 

更改方向将重新创建活动。

+0

是的,这正是我想要做的,但我需要使用onSaveInstance – jaimito

0

问题是,当您更改方向时onCreate()每次都会调用,但您可以通过在Activity标记的AndroidManifest文件中添加Activity的configChanges属性来避免重新创建Activity。

机器人:configChanges =“keyboardHidden |方向”

当您更改屏幕下面的事件的东方
0

已经发生。

===取向的转变===

的onSaveInstanceState - >的onPause - >的onStop - >的onCreate - >在onStart - > onRestoreInstanceState - >的onResume。

所以当你想保存状态,你可以使用onSaveInstanceState()。通常你可以在onCreate()中恢复你的状态。

protected void onSaveInstanceState(Bundle icicle) { 
    super.onSaveInstanceState(icicle); 
    icicle.putString("param", value); 
} 

和用于恢复:

public void onCreate(Bundle icicle) { 
    if (icicle != null){ 
    value = icicle.getString("param"); 
    } 
} 

这样可以节省你想要的颜色值或任何东西,并恢复在onCreate(),并设置为你AlertDialog

+0

你可以发布编辑的代码,我不明白你说的是什么 –

+0

我已经更新了整个我的代码...在此先感谢 – jaimito

+0

是什么问题?你有没有看到任何错误? –