【问题标题】:TextView's text disappearing when device rotated设备旋转时TextView的文本消失
【发布时间】:2012-01-16 11:37:47
【问题描述】:

我正在为 android 编写一个电话拨号器应用程序。我为键盘创建了一个布局,其中包含一个 TextView 和 10 个按钮。按钮作为 10 位数字(0 到 9)的键,TextView 用于根据按下的键显示数字。

在我的应用程序中,我为按下的每个按钮将文本(“0”或“1”等)附加到 TextView。如果我按下 按钮 1、2、3,则 TextView 上的 文本为 123。 问题是,让我们假设屏幕处于横向模式,TextView 包含 123,如果我打开它,在纵向模式下 TextView 上没有文字。

请帮帮我。

【问题讨论】:

  • 您的 TextView 是否包含 id?你使用碎片吗?
  • id 在那里,但我没有使用片段。
  • 在你的 java 类中添加 @Override public void onConfigurationChanged(final Configuration newConfig) { // Ignore orientation change to keep activity from restarting super.onConfigurationChanged(newConfig); } 并在你的清单文件中添加这个 <activity android:name=".ClassName" android:configChanges="orientation|keyboardHidden"></activity> 这应该可以工作。
  • @Mukunda 谢谢它现在的工作。但只有 android:configChanges="orientation 就足够了。

标签: android textview


【解决方案1】:

@jeet 推荐的内容对我不起作用。我不得不添加“screenSize”。这是您应该在活动的 <activity> 节点中的 manifest.xml 中添加的行:

android:configChanges="keyboardHidden|orientation|screenSize"

因此,完整的节点可能如下所示:

<activity
android:name=".YourActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@style/AppTheme.NoActionBar">

【讨论】:

  • 添加screenSize 成功了!调试了几个小时的问题,终于有了解决办法。
【解决方案2】:

请检查方向变化,调用create方法,这需要重新创建所有视图,因此您需要使用以下方法之一:

  1. 使用 onSavedInstance 方法并将组件/视图的状态保存到包中。
  2. 只需在活动标记 android:configChanges="keyboardHidden|orientation" 中的清单文件中使用以下标志 true。如下:

    <activity android:name=".SampleActivity" android:label="@string/app_name"
        android:configChanges="keyboardHidden|orientation">
        ...
    </activity>
    

【讨论】:

    【解决方案3】:

    这是因为 Android 基本上会在您每次旋转设备时破坏 Activity 并重新创建它。这主要是为了允许基于纵向/横向模式的不同布局。

    处理此问题的最佳方法是通过响应 onSavedInstance 事件(在 Android 销毁 Activity 之前调用),然后在标准 onCreate 事件中重新应用这些数据,将您需要保留在 Activity Bundle 中的任何数据存储起来.

    虽然您可以将“方向”添加到 configChanges 属性,但请记住,您基本上是在告诉 Android,您将自己处理与方向更改相关的所有事情 - 包括更改布局等。

    【讨论】:

      【解决方案4】:

      要保留 TextView 的文本,您可以简单地将 TextView 的 freezesText 属性设置true。 如:

          <TextView
          ...
          android:freezesText="true"
          .../>
      

      这是这里公认的答案: Restoring state of TextView after screen rotation?

      【讨论】:

        【解决方案5】:

        如果有人仍然有麻烦......这对我有用

        public class BranjeKP extends AppCompatActivity {
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_branje_kp);
        
            //...
        }
        
        @Override
        protected void onSaveInstanceState(Bundle out) {
            super.onSaveInstanceState(out);
            out.putString("TVNazivPod", TVNazivPodatka.getText().toString());
            out.putString("TVEnotaMere", TVEnotaMere.getText().toString());
        }
        
        @Override
        protected void onRestoreInstanceState(Bundle in) {
            super.onRestoreInstanceState(in);
            TVNazivPodatka.setText(in.getString("TVNazivPod"));
            TVEnotaMere.setText(in.getString("TVEnotaMere"));
        }
        

        您基本上在旋转之前(即调用 onSaveInstanceState 时)将所需的任何值保存到 Bundle 中,而在旋转之后 (onRestoreInstanceState),您只需从 Bundle 中提取所有值。 澄清一下,TVNazivPodatka 和 TVEnotaMere 是 TextView 小部件。

        ...在以下的帮助下 How to prevent custom views from losing state across screen orientation changes

        【讨论】:

          猜你喜欢
          • 2013-12-21
          • 2020-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-24
          相关资源
          最近更新 更多