【问题标题】:Save object's last value-state保存对象的最后一个值状态
【发布时间】:2014-02-27 10:48:45
【问题描述】:

简介

我有一个绘图活动,从调色板中选择颜色我打开一个DialogFragment

QUID

第一次打开DialogFragment 时,我将bnColor 对象设置为实例,并为其赋予第一个按钮的值,因此,在调色板中,第一个按钮将显示为选中状态,这是我要绘制的颜色.

但是,如果我在调色板中选择另一种颜色,下次我再次进入调色板时,我希望这种新颜色显示为已选择。取而代之的是,总是选择第一种颜色。

我知道这是因为每次我输入DialogFragment 时,bnColor 对象都是空的,所以它总是获得第一个颜色的值。为了解决这个问题,我需要做一些事情,比如保存 bnColor 对象的最后一个状态,所以当我进入 dialogFragment 时,它会检查是否是第一次并且它为空,或者我之前输入过并且有一个先前的值已保存。

但我从来没有做过这样的事情,我不知道该怎么做。

这是 dialogFragment 上的相关代码:

private ImageButton bnColor;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View view = inflater.inflate(R.layout.palette, null, false);

    if (bnColor == null) {
        LinearLayout drawLayout = (LinearLayout) view.findViewById(R.id.paint_colors);
        bnColor = (ImageButton) drawLayout.getChildAt(0);
        bnColor.setImageDrawable(getResources().getDrawable(R.drawable.button_pressed));
    }

【问题讨论】:

    标签: android layout android-dialogfragment savestate


    【解决方案1】:

    你为什么不序列化 bnColor object 本身以获得它的最后一个状态?

    转到 here ,您会在源代码中获得一个名为 CustomSharedPreference 的文件,只需将其下载并复制到您的身边并保存该对象,就像您保存任何其他首选项对象一样

    CustomSharedPreference mPrefs = new CustomSharedPreference(                 getApplicationContext());
    OR
    CustomSharedPreference mPrefs = new CustomSharedPreference( "prefsName",Context.MODE_PRIVATE, getApplicationContext());
    
    //Save your Object
    mPrefs.putObject("object", new Object());
    //Retrieve it and store it
    Object o = (Object)mPrefs.getObject("object", null);
    

    注意:该视图不能序列化,因此您需要创建一个像 implements Serializable 类这样的自定义 ImageButton 类,以及 @987654326 中的所有对象@ 应该已经实现了Serializable 接口

    public class MyImageButton extends ImageButton implements Serializable {
    
        public MyImageButton(Context context, AttributeSet attrs) {
            super(context, attrs);
    
        }
    
    }
    

    并在您的 XML 中使用它

    <com.example.MyImageButton
        android:id="@+id/MyimageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon" />
    

    【讨论】:

      【解决方案2】:

      我以这种方式保存状态。

      public class MyDialogFragment extends DialogFragment {
      
          //this is the default value, set when the dialog is created
          private String myValue = "any initial String"; 
          private TextEdit myTextEdit;
      
          @Override
          public Dialog onCreateDialog(Bundle savedInstanceState) {
      
              //construct the dialog
              AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
              LayoutInflater inflater = getActivity().getLayoutInflater();
              ViewGroup myView = (ViewGroup)inflater.inflate(R.layout.my_view, null);
              builder.setView(myView);
      
              //find the view and set the value
              myTextEdit = (TextView)myView.findViewById(R.id.my_text_edit);
              myTextEdit.setText(myValue);
      
              return builder.create();
          }
      
          @Override
          public void onDismiss(DialogInterface dialog) {
              super.onDismiss(dialog);
      
              //when the dialog is dismissed, save the users input and overwrite the initial value
              myValue = myTextEdit.getText();
      
          }
      

      【讨论】:

        【解决方案3】:

        你可以这样做

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState)
        {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            View view = inflater.inflate(R.layout.palette, null, false);
        
            ImageButton  bnColor;
            if(bnColor == null)
            {
                LinearLayout drawLayout = (LinearLayout) view.findViewById(R.id.paint_colors);
                if(savedInstanceState!=null)
                    bnColor= (ImageButton) getResources().getLayout(savedInstanceState.getInt("color"));
                else
                bnColor = (ImageButton) drawLayout.getChildAt(0);
        
                bnColor.setImageDrawable(getResources().getDrawable(R.drawable.button_pressed));
            }
        
            int value= bnColor.getId();
        
            savedInstanceState.putInt("color", value);
            onSaveInstanceState(savedInstanceState);
        }
        

        【讨论】:

        • 1)onSaveInstanceState 什么都不做。因此它不起作用。 2) savedInstanceState 在大多数情况下将为空。只有当系统重新创建对话框时,它才不会为空,而不是当您关闭并显示它时。
        • @Doctoror Drive 你能在新答案中提供解决方案吗?
        • @masmic_87 将颜色保存到 SharedPreferences 并在 onCreateDialog 中恢复。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-26
        相关资源
        最近更新 更多