保存对象的最后一个值状态

时间:2014-02-27 10:48:45

标签: android layout android-dialogfragment savestate

引言

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

enter image description here

嚼食

我第一次打开DialogFragment时,我将bnColor对象设置为静态,并给它第一个按钮的值,因此,在调色板中,第一个按钮将显示为已选中,这是我的颜色会油漆。

但是如果我然后在调色板中选择另一种颜色,下次再次进入调色板时,我希望这种新颜色显示为选中状态。取而代之的是,第一种颜色始终处于选中状态。

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

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

这是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));
    }

3 个答案:

答案 0 :(得分:0)

为什么不序列化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类,也可以创建一个自定义的ImageButton类。 MyImageButton应该已经实现了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" />

答案 1 :(得分:0)

我以这种方式保存状态。

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();

    }

答案 2 :(得分:-1)

你可以做这样的事情

@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);
}
相关问题