gpt4 book ai didi

Android TextView 内容丢失,即使 android :freezesText is set to true

转载 作者:行者123 更新时间:2023-11-29 14:15:51 26 4
gpt4 key购买 nike

我有一个包含两个 fragment 的 Activity 。该 Activity 具有不同的纵向和横向布局,但 fragment 保持不变(使用标签创建并重复使用)。在其中一个 fragment 中,我有 xml 定义的 TextView ,它有 android:freezesText="true"行,但它在屏幕旋转后仍然丢失文本。为什么会发生这种情况?

fragment 的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
android:id="@+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dip10"
android:freezesText="true"
android:gravity="center"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

fragment 的类:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DisplayFrag extends Fragment {

@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.display_frag, container, false);
TextView tvDisplay = (TextView) view.findViewById(R.id.tvDisplay);
tvDisplay.setText(getArguments().getString(Keys.NAME)
+ ", how are you? your phone number is " + getArguments().getString(Keys.PHONE));
return view;
}
}

Activity :

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends FragmentActivity {

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

FragmentManager fragMan = getSupportFragmentManager();
InputFrag inputFrag = (InputFrag) fragMan.findFragmentByTag(Keys.TAG_INPUT_FRAG);
if (inputFrag == null) {
fragMan.beginTransaction().add(R.id.hook1, new InputFrag(), Keys.TAG_INPUT_FRAG)
.commit();
} else {
fragMan.beginTransaction().replace(R.id.hook1, inputFrag, Keys.TAG_INPUT_FRAG).commit();
}
}

public void submit(View view) {
FragmentManager fragMan = getSupportFragmentManager();
DisplayFrag displayFrag = (DisplayFrag) fragMan.findFragmentByTag(Keys.TAG_DISPLAY_FRAG);
if (displayFrag == null) {
displayFrag = new DisplayFrag();
displayFrag = putBundle(displayFrag);
fragMan.beginTransaction().add(R.id.hook2, displayFrag, Keys.TAG_DISPLAY_FRAG).commit();
} else {
fragMan.beginTransaction().replace(R.id.hook2, displayFrag, Keys.TAG_DISPLAY_FRAG)
.commit();
}

}

private DisplayFrag putBundle(DisplayFrag displayFrag) {
Bundle bundle = new Bundle();
bundle.putString(Keys.NAME, ((EditText) findViewById(R.id.etName)).getText().toString());
bundle.putString(Keys.PHONE, ((EditText) findViewById(R.id.etPhone)).getText().toString());
displayFrag.setArguments(bundle);
return displayFrag;
}
}

(提交响应另一个 fragment 上的按钮,行为正确)。

最佳答案

找到解决方案:根本不需要 .replace() fragment 。这是 MainActivity 和 DisplayFrag 的新代码:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends FragmentActivity {

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

FragmentManager fragMan = getSupportFragmentManager();
InputFrag inputFrag = (InputFrag) fragMan.findFragmentByTag(Keys.TAG_INPUT_FRAG);
if (inputFrag == null) {
fragMan.beginTransaction().add(R.id.hook1, new InputFrag(), Keys.TAG_INPUT_FRAG)
.commit();
}

DisplayFrag displayFrag = (DisplayFrag) fragMan.findFragmentByTag(Keys.TAG_DISPLAY_FRAG);
if (displayFrag == null) {
fragMan.beginTransaction().add(R.id.hook2, new DisplayFrag(), Keys.TAG_DISPLAY_FRAG)
.commit();
}
}

public void submit(View v) {
String name = ((EditText) findViewById(R.id.etName)).getText().toString();
String phone = ((EditText) findViewById(R.id.etPhone)).getText().toString();
if (!"".matches(name) && !"".matches(phone)) {
FragmentManager fragMan = getSupportFragmentManager();
DisplayFrag displayFrag =
(DisplayFrag) fragMan.findFragmentByTag(Keys.TAG_DISPLAY_FRAG);
if (displayFrag != null) {
displayFrag.display(name, phone);
}
}
}
}

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DisplayFrag extends Fragment {

@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.display_frag, container, false);
return view;
}

public void display(String name, String phone) {
TextView tvDisplay = (TextView) getActivity().findViewById(R.id.tvDisplay);
tvDisplay.setText(name + ", how are you? your phone number is " + phone);
}

}

暂时接受我的回答。如果有人有任何其他答案、建议或知识可以分享,我会很乐意接受不同的答案。

关于Android TextView 内容丢失,即使 android :freezesText is set to true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14251288/

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