1

我有一个包含大量 EditText 的巨大表单的活动。如果用户将屏幕从纵向旋转到横向,反之亦然,EditTexts 中的所有数据都会丢失。是否有某种方法可以保留数据,以便在更改方向时不会丢失数据?

我创建了 2 个布局,一个用于纵向和横向。对于两种布局中的所有视图,我也有相同的 ID。

提前致谢。

4

4 回答 4

2

是的。您可以使用以下代码获取 EditTexts 的数据:

onCreate()方法上,

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        setContentView(R.layout.<your_landscape_layout>);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.<your_portrait_layout>);
        break;
    }
    setButtonClickListeners();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:

        setContentView(R.layout.<your_landscape_layout>);
        setButtonOnClickListeners();
        initializeViews();
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.<your_portrait_layout>);
        setButtonOnClickListeners();
        initializeViews();
        break;
    }
}

@SuppressWarnings("deprecation")
@Override
public Object onRetainNonConfigurationInstance() {
    return super.onRetainNonConfigurationInstance();
}
 }

setButtonOnClickListeners()中,您启动所有EditTexts并使用initializeViews()获取数据EditTexts并显示setText()

于 2013-06-27T11:01:25.820 回答
2

通过在您的活动中实施以下方法来保存您的状态

/* simple method to create a key for a TextView using its id */
private String keyForTextView(TextView txt){
    return "textView"+txt.getId();
}


/* go through all your views in your layout and save their values */
private void saveTextViewState(ViewGroup rootView, Bundle bundle){
    int children = rootView.getChildCount();
    for (int i = 0; i < children; i++) {
        View view = rootView.getChildAt(i);
        if (view instanceof TextView){
            TextView txt = (TextView)view;
            if (txt.getText() != null){
                bundle.putString(keyForTextView(txt), txt.getText().toString());
            }

        }else if (view instanceof ViewGroup){
            saveTextViewState((ViewGroup)view, bundle);
        }
    }
}


@Override
protected void onSaveInstanceState(Bundle outState) {

    View root = findViewById(R.id.my_root_view); //find your root view of your layout
    saveTextViewState(root, outState); //save state 

    super.onSaveInstanceState(outState);
}

然后检索活动的 onCreate 方法中的值。

 /* go through all your views in your layout and load their saved values */
 private void loadTextViewState(ViewGroup rootView, Bundle bundle){
    int children = rootView.getChildCount();
    for (int i = 0; i < children; i++) {
        View view = rootView.getChildAt(i);
        if (view instanceof TextView){
            TextView txt = (TextView)view;
            String saved = bundle.getString(keyForTextView(txt));
            txt.setText(saved); 

        }else if (view instanceof ViewGroup){
            loadTextViewState((ViewGroup)view, bundle);
        }
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //... inflate your UI here

    if (savedInstanceState != null){
         View root = findViewById(R.id.my_root_view); //find your root view
         loadTextViewState(root, savedInstanceState); //load state 
    }
}

为此,所有文本框都必须在横向和纵向布局中都有 id。

于 2013-06-27T11:05:11.183 回答
0

添加android:configChanges="orientation"您的清单文件。标签内,

    <activity
        android:name=".YourActivity"
        android:configChanges="orientation|keyboardHidden" >
    </activity>

谢谢....

于 2013-06-27T11:04:23.680 回答
0

您需要覆盖 onSaveInstanceState(Bundle savedInstanceState)并将要更改的应用程序状态值写入 Bundle 参数,如下所示:

此方法将 UI 状态更改保存到 savedInstanceState。如果进程被杀死并重新启动,这个包将被传递给 onCreate。

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);

  EditText mEdittext =(EditText) findViewById(R.id.YourEdittextId);
  savedInstanceState.putString("MyString", mEdittext.getText().toString());
  ...
}

Bundle 本质上是一种存储 NVP(“名称-值对”)映射的方法,它将被传递给 onCreate 和 onRestoreInstanceState,您可以在其中提取如下值:

此方法从 savedInstanceState 恢复 UI 状态 此捆绑包也已传递给 onCreate。

   @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);

      String myString = savedInstanceState.getString("MyString");
     EditText mEdittext =(EditText) findViewById(R.id.YourEdittextId);
      mEdittext.setText(myString);
    }

您必须使用此技术为您的应用程序(选择、文本等)存储实例值,以了解更多信息请参阅此链接

于 2013-06-27T11:05:20.137 回答