【问题标题】:Contents of textView change when contents are selectable当内容可选择时,textView 的内容会发生变化
【发布时间】:2015-02-22 09:13:30
【问题描述】:

我目前正在处理一个项目,该项目需要我根据数组的内容将 textView 对象动态添加到 LinearLayout。

chapter = Home.chapters.get(index);
    layout.removeAllViews();
    String dataText = chapter.content;
    String[] dataArray = dataText.split("~");
    for (int i = 0; i < dataArray.length; i++) {
        String paragraph = dataArray[i];
        String outputParagraph = paragraph.replace("`", "\n");
        View view = getLayoutInflater().inflate(R.layout.ttc_text, null);
        final TextView tv = (TextView) view.findViewById(R.id.textView);
        tv.setText(outputParagraph);

        //tv.setTextIsSelectable(true);

        layout.addView(view);

    }

    TextView titleView = (TextView) findViewById(R.id.title);
    titleView.setText("Chapter " + chapter.title);

    layout.setOnTouchListener(new OnSwipeTouchListener(getBaseContext()) {

        public void onSwipeRight() {
            retreatBackward();
        }

        public void onSwipeLeft() {
            advanceForward();
        }
    });

我已配置 saveOnItemStateChanged 以便在屏幕旋转时恢复 textViews 的正确内容。

if (savedInstanceState != null) {
        index = savedInstanceState.getInt("Chapter");
    }

 @Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt("Chapter", index);

    super.onSaveInstanceState(savedInstanceState);
}

但是,无论何时旋转屏幕,父布局中的 textView 都不会显示其原始内容。而是原来布局中相同数量的textView都显示相同的内容,也就是最终添加到布局中的textView的内容。

仅当通过setTextIsSelectable() 函数或通过 XML 标记将 TextViews 中的文本指定为可选时,才会出现此问题。因此,文本的可选择性一定会影响旋转后重新填充 TextViews 内容的方式,尽管我似乎无法确定确切原因。任何帮助将不胜感激。

【问题讨论】:

  • 为什么不使用表格布局?

标签: android android-layout textview screen-rotation


【解决方案1】:

我认为问题是由于您设备的配置更改所致。当您旋转设备时,会发生配置更改事件。将以下代码添加到您的 Manifest.xml 和您的 activity 类中。希望这能解决您的问题。

Manifest.xml 文件的更改 --->(假设 XYZ 是您的活动类)

 <activity android:name=".activities.XYZ"
              android:configChanges="orientation|screenSize"/>

将此添加到您的 XYZ 活动类中 --->

  @Override
   public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
  }

【讨论】:

  • 很高兴能为您提供帮助。 :)
【解决方案2】:

第一个

super.onSavedInstanceState(...) 应该是第一个被调用的东西。您的“保存”内容应该在后面。

第二次

您很可能需要保存所有文本视图,以便重新填充它们,或者保存数组并基于此重新填充。

我发现这个网站非常适合解释保存实例状态处理的来龙去脉。

http://www.intertech.com/Blog/saving-and-retrieving-android-instance-state-part-1/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 2022-01-27
    • 2011-10-15
    • 2013-09-14
    • 1970-01-01
    • 2018-03-30
    • 2014-02-22
    • 1970-01-01
    相关资源
    最近更新 更多