3

我正在构建我的第一个应用程序,几乎完成了。(耶!)现在我所要做的就是为 onConfigurationChange 设置 Java,以便在更改方向以及拉出键盘时使用。首先,问题是当方向改变时,所有东西都会被删除,因为 setContentView 方法放置了一个新的布局。这当然是有道理的,但是我想知道是否有任何方法或可能的解决方法可以让 edittext 和 textview 值在方向/键盘更改时保持不变。我尝试了各种方法,例如在 setContentView 之前获取字符串值,但是我发现这只会导致 NullPointerException。

基本上我试图保持edittext和textview的值与方向或任何变化相同。

这是我的代码的简要摘要,以供参考

这不是实际的代码,只是我正在做的总结。

    public class MainActivity extends Activity {

      protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     //do some stuff that is the core of the app. Nothing that would impact this question though

    }

      public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);

     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){

        setContentView(R.layout.landscape);
            // same code that was within the onCreate. Just doing stuff related to  my  app 

    }if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

        setContentView(R.layout.main);
            // same code that was within the onCreate. Just doing stuff related to  my  app
    }
         }
    ' }

是的,我确实在我的 AndroidManifest 文件中写了 android:configChanges="orientation|keyboardHidden" 该应用程序正常工作,我只是没有更新文本 viwes 和 edittexts。谢谢!

4

2 回答 2

0

但是我想知道是否有任何方法或可能的解决方法可以让 edittext 和 textview 值在方向/键盘更改时保持不变。

这样做……不。当您调用setContentView()它时,它将所有内容都设置为默认值,因此TextViews 将具有其默认文本等...正如您所说,您可以尝试保存Strings并再次设置它们,但这会变得混乱。您可以使用“Android 推荐”的方式处理它并使用 处理运行时更改。但是,我目前自己处理所有这些,但在更改setContentView()期间我不打电话。orientation

如果您告诉我们您为什么这样做,那么我们也许可以为您提供更好的解决方案。但我不建议setContentView()像这样多次调用

小例子

@Override
    public void onConfigurationChanged(Configuration newConfig) {
      newConfig = Globals.getUserLanguage(this);
      super.onConfigurationChanged(newConfig);
      if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
      {
          btnSave = (Button) findViewById(R.id.lsSaveBtn);
          Button btnHide = (Button) findViewById(R.id.button1);
          btnHide.setVisibility(Button.GONE);
          btnSave.setVisibility(Button.VISIBLE);
    //    btnSave.setOnClickListener();
      }
      else
      {
          btnSave = (Button) findViewById(R.id.button1);    
          Button btnHide = (Button) findViewById(R.id.lsSaveBtn);
          btnHide.setVisibility(Button.GONE);
          btnSave.setVisibility(Button.VISIBLE);
      }
    }
于 2013-06-14T02:38:52.997 回答
0

您可以将值保存在savedInstanceState.

当您的活动开始停止时,系统会调用onSaveInstanceState()以便您的活动可以保存状态。

至于恢复值,你可以使用

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

或者

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}

您还可以将布局放在layout肖像和风景的文件夹中,layout-land这样您就不需要setContentView()onConfigurationChanged()每次应用程序restart活动时调用

链接:活动再造

于 2013-06-14T02:59:16.170 回答