0

そのため、EditTexts は向きを変更するときにテキストを保存する必要があります。この特定のフラグメントでは、そうではありません。何故ですか?

私のフラグメント:

 public class LoginFragment extends Fragment {

    /**
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
     *      android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.login, container, false);

    }

    /**
     * 
     * @see android.support.v4.app.Fragment#onStart()
     */
    @Override
    public void onStart() {
    super.onStart();

    getView().findViewById(R.id.login_ok).setOnClickListener(
        new OnClickListener() {

            public void onClick(View v) {
            startActivity(new Intent(getActivity(),
                StartActivity.class));
            }
        });
    }
}

xmlの私の編集テキスト:

 <EditText
        android:id="@+id/login_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:background="@drawable/roundedcorner"
        android:ems="10"

        android:hint="@string/email"
        android:inputType="textEmailAddress"
        android:padding="8dp" >

        <requestFocus />
    </EditText>





    <EditText
        android:id="@+id/login_pw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:background="@drawable/roundedcorner"
        android:ems="10"

        android:hint="@string/password"
        android:inputType="textPassword"
        android:padding="8dp" />

これは私のアクティビティの onCreate メソッドです:

super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.login_frame, new LoginFragment());
ft.commit();`
4

3 に答える 3

2

方向が変わるとアクティビティが再作成されます。そうです。ただし、UI 要素の状態を処理する必要はありません。

Android フレームワークのほぼすべてのウィジェットがこのメソッドを適切に実装しているため、UI に表示される変更は自動的に保存され、アクティビティが再作成されたときに復元されます。たとえば、EditText ウィジェットはユーザーが入力したテキストを保存し、CheckBox ウィジェットはチェックされているかどうかに関係なく保存します。必要な作業は、状態を保存するウィジェットごとに一意の ID (android:id 属性を使用) を指定することだけです。ウィジェットに ID がない場合、システムはその状態を保存できません。

から: http://developer.android.com/guide/components/activities.html#SavingActivityState

注:ユーザーが [戻る] ボタンを使用した場合でも、EditText 値を保持したい場合は、永続的なストレージが必要です。onSaveInstanceState は呼び出されません。

簡単な例を次に示します (EditText 値は保持されます)。

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivty_main); 
    }
}


public class EditFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_edit, container);
    }
}

.XML ファイル

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_id"
    android:name="com.example.fragementtest.EditFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
    </EditText>

</LinearLayout>
于 2012-09-17T14:36:23.633 に答える
1

さて、この問題が発生しました。これは、FragmentTransactionを使用してFragmentをFrameLayout(一意のIDを持つ)に追加したためです。

これで、フラグメントをXMLで一意のIDでもう一度宣言し、機能します。

コンテナに一意のIDがある場合、FragmentTransactionを追加すると機能しない理由を実際に説明することはできませんが、少なくとも現在は機能しています。

于 2012-09-17T15:31:44.937 に答える
0

TextEdit フィールドと TextView フィールドの内容を確実に保持する簡単な方法は、freezesText 属性を設定することです。

于 2018-09-25T01:16:17.467 に答える