Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中的destroyview上保存UI元素的状态_Android_Android Fragments - Fatal编程技术网

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

在android中的destroyview上保存UI元素的状态,android,android-fragments,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

我有一个包含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>
}


我试图用onDestroyview方法保存内容。但我无法使用findViewbyId函数,因为它会抛出错误。非常感谢您的帮助

您能否将从findViewById检索到的EditText对象的实例保存为在onDestroyView中访问的类变量?@CPUTerminator如果我要创建编辑文本数组,请在OnCreateview方法中使用findViewById?然后在onDestroyViewYeah中使用,我会这样做。如果您不想,您不需要将它们填充到数组中,但是是的,这是完全有效的。关于在UI元素中保存和填充数据,我将使用方法对onSaveInstanceState和onRestoreInstanceState,因为它们是专门为开发人员根据需要保存和恢复数据而调用的。如果不使用所传递的Bundle对象,这无关紧要。
@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;
 }