保留屏幕方向变化的数据

时间:2013-06-27 10:57:03

标签: android android-layout

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

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

提前致谢。

4 个答案:

答案 0 :(得分: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()显示

答案 1 :(得分: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。

答案 2 :(得分:0)

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

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

...谢谢

答案 3 :(得分: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);
    }

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