简体   繁体   English

Android:TextView.setText()的结果被片段覆盖

[英]Android: Result of TextView.setText() overridden by fragment

I'm a bit new to using fragments, and am having an issue with setting the text of a TextView within a fragment. 我对使用片段有些陌生,并且在片段中设置TextView的文本时遇到了问题。

Right now, I have a single Activity with a common set of six buttons, along with two fragments which are displayed in a LinearLayout. 现在,我有一个带有六个按钮的通用集合的Activity,以及在LinearLayout中显示的两个片段。 I am able to use the buttons to replace the fragment in the LinearLayout successfully. 我能够使用按钮成功替换LinearLayout中的片段。 However, I'm noticing some strange behavior related to changing a TextView in those two fragments. 但是,我注意到这两个片段中与更改TextView有关的一些奇怪行为。

I have a method in the fragments' class, called setTimer(), which attempts to change the text in the TextView. 我在片段类中有一个名为setTimer()的方法,该方法试图更改TextView中的文本。 The strange thing is, the method works successfully. 奇怪的是,该方法成功运行。 However, a split second later, the text reverts back to the default text in the TextView contained in the fragment's layout .xml file (which is a blank string). 但是,稍后,该文本将恢复为片段布局.xml文件(为空白字符串)中包含的TextView中的默认文本。

I've tried calling the setTimer() method both before and after I replace the fragment in the LinearLayout, and the results are the same either way. replace LinearLayout中的片段之前和之后,我都尝试过调用setTimer()方法,并且两种方法的结果都是相同的。 How can I change this TextView's contents without having them overridden moments later? 我如何更改此TextView的内容而又不会在稍后覆盖它们? Thank you! 谢谢!

MainActivity.java's onCreate() method: 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: 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;
    }
}

Finally, the changeFragment() method, which is called when the buttons are pressed: 最后,当按下按钮时将调用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();
}

The problem is that the OnCreateView() method of the fragment is called after the setTimer() is called. 问题在于,在调用setTimer()之后,将调用片段的OnCreateView()方法。

An easy way to solve this is to first call fragment.setTimerValue(value) when you create the fragment. 一种简单的解决方法是在创建片段时首先调用fragment.setTimerValue(value)

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

Then at the end of OnCreateView() method do: 然后在OnCreateView()方法的末尾执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM