在android中的destroyview上保存UI元素的状态

时间:2014-12-28 19:01:51

标签: android android-fragments

我有一个包含10个编辑文本的片段,我希望在片段被销毁时存储那些文本,并在再次创建片段时进行恢复。布局是

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp">

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp">

        <EditText
            android:id="@+id/editText"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:background="@null"
            android:cursorVisible="false"
            android:inputType="textNoSuggestions"
            android:textColor="#006895" />

        <Switch
            android:id="@+id/switch1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="111dp"
            android:layout_marginRight="10dp"
            android:layout_marginStart="111dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll1"
        android:paddingTop="10dp">

        <EditText
            android:id="@+id/editText2"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:background="@null"
            android:cursorVisible="false"
            android:inputType="textNoSuggestions"
            android:textColor="#006895" />

        <Switch
            android:id="@+id/switch2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="111dp"
            android:layout_marginStart="111dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll2"
        android:paddingTop="10dp">

        <EditText
            android:id="@+id/editText3"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:background="@null"
            android:cursorVisible="false"
            android:inputType="textNoSuggestions"
            android:textColor="#006895" />

        <Switch
            android:id="@+id/switch3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="111dp"
            android:layout_marginStart="111dp"
            android:textColor="#006895" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll3"
        android:paddingTop="10dp">

        <EditText
            android:id="@+id/editText4"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:background="@null"
            android:cursorVisible="false"
            android:inputType="textNoSuggestions"
            android:textColor="#006895" />

        <Switch
            android:id="@+id/switch4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="111dp"
            android:layout_marginStart="111dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll4"
        android:paddingTop="10dp">

        <EditText
            android:id="@+id/editText5"
            android:layout_width="115dp"

            android:layout_height="wrap_content"
            android:background="@null"
            android:cursorVisible="false"

            android:inputType="textNoSuggestions"
            android:textColor="#006895" />


        <Switch
            android:id="@+id/switch5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginLeft="111dp"
            android:layout_marginStart="111dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll5"
        android:paddingTop="10dp">

        <EditText
            android:id="@+id/editText6"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:background="@null"
            android:cursorVisible="false"
            android:inputType="textNoSuggestions"
            android:textColor="#006895" />

        <Switch
            android:id="@+id/switch6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginLeft="111dp"
            android:layout_marginStart="111dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll6"
        android:paddingTop="10dp">

        <EditText
            android:id="@+id/editText7"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:background="@null"
            android:cursorVisible="false"
            android:inputType="textNoSuggestions"
            android:textColor="#006895" />

        <Switch
            android:id="@+id/switch7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="111dp"
            android:layout_marginStart="111dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll7"
        android:paddingTop="10dp">

        <EditText
            android:id="@+id/editText8"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:background="@null"
            android:cursorVisible="false"
            android:inputType="textNoSuggestions"
            android:textColor="#006895" />

        <Switch
            android:id="@+id/switch8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="111dp"
            android:layout_marginStart="111dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll8"
        android:paddingTop="10dp">

        <EditText
            android:id="@+id/editText9"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:background="@null"
            android:cursorVisible="false"
            android:inputType="textNoSuggestions"
            android:textColor="#006895" />

        <Switch
            android:id="@+id/switch9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="111dp"
            android:layout_marginStart="111dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll9"
        android:paddingTop="10dp">

        <EditText
            android:id="@+id/editText10"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:background="@null"
            android:cursorVisible="false"
            android:inputType="textNoSuggestions"
            android:textColor="#006895" />

        <Switch
            android:id="@+id/switch10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="111dp"
            android:layout_marginStart="111dp" />
    </LinearLayout>

</RelativeLayout>

截至目前,我点击时保存编辑文本的值。它无效,因为我必须输入文本,然后再次单击它以保存它。我从编辑文本中获取文本并将其保存在共享首选项

@Override
public void onClick(View v) {
SharedPreferences prefs = getActivity().getSharedPreferences("hall", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
String name;
EditText tv = (EditText) v.findViewById(v.getId());
switch (v.getId()) {
    case R.id.editText:
        name = tv.getText().toString();
        editor.putString("switch1", name);
        editor.commit();
        Log.d("switch1", name);
    break;
    case R.id.editText2:
        name = tv.getText().toString();
        editor.putString("switch2", name);
        editor.commit();
        Log.d("switch2", name);
    break;
    case R.id.editText3:
        name = tv.getText().toString();
        editor.putString("switch3", name);
        editor.commit();
        Log.d("3", name);
    break;
    case R.id.editText4:
        name = tv.getText().toString();
        editor.putString("switch4", name);
        editor.commit();
        Log.d("switch4", name);
    break;
    case R.id.editText5:
        name = tv.getText().toString();
        editor.putString("switch5", name);
        editor.commit();
        Log.d("switch5", name);
    break;
    case R.id.editText6:
        name = tv.getText().toString();
        editor.putString("switch6", name);
        editor.commit();
        Log.d("switch6", name);
    break;
    case R.id.editText7:
        name = tv.getText().toString();
        editor.putString("switch7", name);
        editor.commit();
        Log.d("switch7", name);
    break;
    case R.id.editText8:
        name = tv.getText().toString();
        editor.putString("switch8", name);
        editor.commit();
        Log.d("switch8", name);
    break;
    case R.id.editText9:
        name = tv.getText().toString();
        editor.putString("switch9", name);
        editor.commit();
         Log.d("switch9", name);
    break;
    case R.id.editText10:
        name = tv.getText().toString();
        editor.putString("switch10", name);
        editor.commit();
        Log.d("switch10", name);
    break;
 }

}

我试图在onDestroyview方法中保存内容。但我无法使用findViewbyId()函数,因为它会抛出错误。任何帮助表示赞赏

0 个答案:

没有答案