gpt4 book ai didi

Android保存和恢复TextView文本ViewPager fragment

转载 作者:行者123 更新时间:2023-11-29 15:40:07 25 4
gpt4 key购买 nike

在我的 ViewPager 中,我有一些 fragment ,在 TextView 的页面文本之间切换后,我动态更改了一些 TextView 文本,并且我无法从 instance

保存和恢复它们

这是我的代码,不工作

private       String                       value1;
private String value2;
private String value3;

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

//@formatter:off
context = getActivity().getBaseContext();
activity = getActivity();
//@formatter:on
}
@Override
public void onViewStateRestored(@Nullable Bundle inState) {
super.onViewStateRestored(inState);
if (inState != null) {
inState.getString("value1", value1);
inState.getString("value2", value2);
inState.getString("value3", value3);

sahmiyeh_value_1.setText(value1);
sahmiyeh_value_2.setText(value2);
sahmiyeh_value_3.setText(value3);
}
}

@Override
public void onSaveInstanceState(@Nullable Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("value1", sahmiyeh_value_1.getText().toString());
outState.putString("value2", sahmiyeh_value_2.getText().toString());
outState.putString("value3", sahmiyeh_value_3.getText().toString());
}

最佳答案

你可以试试freezesText

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

来自文档:

If set, the text view will include its current complete text inside of its frozen icicle in addition to meta-data such as the current cursor position. By default this is disabled; it can be useful when the contents of a text view is not stored in a persistent place such as a content provider

关于Android保存和恢复TextView文本ViewPager fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41974477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com