java - Android:TextView.setText() 的结果被 fragment 覆盖

标签 java android android-fragments textview

我对使用 fragment 有点陌生,并且在 fragment 中设置 TextView 的文本时遇到问题。

现在,我有一个 Activity,其中包含一组常见的六个按钮,以及两个显示在 LinearLayout 中的 fragment 。我能够使用按钮成功替换 LinearLayout 中的 fragment 。但是,我注意到与更改这两个 fragment 中的 TextView 相关的一些奇怪行为。

我在 fragment 类中有一个名为 setTimer() 的方法,它尝试更改 TextView 中的文本。奇怪的是,这个方法竟然成功了。然而,片刻之后,文本恢复为 fragment 布局 .xml 文件中包含的 TextView 中的默认文本(这是一个空白字符串)。

我尝试在替换 LinearLayout 中的 fragment 之前和之后调用 setTimer() 方法,结果都是相同的。如何更改此 TextView 的内容而不让它们稍后被覆盖?谢谢!

MainActivity.java 的 onCreate() 方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        FocusFragment initialFragment = new FocusFragment();
        initialFragment.setFragmentType( Constants.FRAGMENT_WORK );
        initialFragment.setTimer( 25 );
        getFragmentManager().beginTransaction()
                .add( R.id.linearLayout_fragmentHolder, initialFragment, "fragment_work" )
                .commit();
    }
}

FocusFragment.java:

public class FocusFragment extends Fragment {

    int fragment_type;
    static TextView timer;

    final String TAG = "FocusFragment";

    public FocusFragment() {

    }

    public void setFragmentType( int fragment_type ) {
        this.fragment_type = fragment_type;
    }

    public int getFragmentType() {
        return this.fragment_type;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View rootView;
        if ( getFragmentType() == Constants.FRAGMENT_WORK ) {
            rootView = inflater.inflate( R.layout.fragment_work, container, false );
        } else {
            rootView = inflater.inflate( R.layout.fragment_break, container, false );
        }
        timer = ( TextView ) rootView.findViewById( R.id.textView_timer );
        return rootView;
    }

    public boolean setTimer( int timerValue ) {
        if ( timer != null ) {
            if ( timerValue < 10 ) {
                timer.setText( "0" + timerValue + ":00" );
            } else {
                timer.setText( timerValue + ":00" );
            }
            Log.d( TAG, "Timer text set successfully." );
            return true;
        }
        Log.w( TAG, "WARNING: setTimer() couldn't find the timer TextView!" );
        return false;
    }
}

最后,changeFragment() 方法,按下按钮时调用:

public void changeFragment( String fragmentName, int timerValue ){
    FocusFragment fragment = new FocusFragment();
    if ( fragmentName == "fragment_work" ) {
        fragment.setFragmentType( Constants.FRAGMENT_WORK );
    } else {
        fragment.setFragmentType( Constants.FRAGMENT_BREAK );
    }
    fragment.setTimer( timerValue );
    getFragmentManager().beginTransaction()
            .replace( R.id.linearLayout_fragmentHolder, fragment, fragmentName )
            .commit();
}

最佳答案

问题是在调用setTimer()之后调用了fragment的OnCreateView()方法。

解决此问题的一个简单方法是在创建 fragment 时首先调用 fragment.setTimerValue(value)

void setTimerValue(int value){
    this.timerValue = value;
}

然后在OnCreateView()方法的末尾执行:

OnCreateView(){
    ...
    setTimer(timerValue);
    return rootView;
}

关于java - Android:TextView.setText() 的结果被 fragment 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24553926/

相关文章:

java - Android 中的单例与应用程序上下文?

java - 无法找到默认 Activity 类名称

android - 如何打开 WiFi 扫描仪?

android - 扩展 actionBar-ActionView 并显示新的 Fragment

android - fragment 中的选项卡

Java 转换 BufferedImage "Subimage"由 4 个点定义

java - 列数与第 1 行 java 的值数不匹配

JavaFX 将一个形状拖放到另一个形状上

android - 两个 TextWatcher 创建一个无限循环

java - 使用新 fragment 动态更新 ViewPager