簡體   English   中英

Android:TextView.setText()的結果被片段覆蓋

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

我對使用片段有些陌生,並且在片段中設置TextView的文本時遇到了問題。

現在,我有一個帶有六個按鈕的通用集合的Activity,以及在LinearLayout中顯示的兩個片段。 我能夠使用按鈕成功替換LinearLayout中的片段。 但是,我注意到這兩個片段中與更改TextView有關的一些奇怪行為。

我在片段類中有一個名為setTimer()的方法,該方法試圖更改TextView中的文本。 奇怪的是,該方法成功運行。 但是,稍后,該文本將恢復為片段布局.xml文件(為空白字符串)中包含的TextView中的默認文本。

replace LinearLayout中的片段之前和之后,我都嘗試過調用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()之后,將調用片段的OnCreateView()方法。

一種簡單的解決方法是在創建片段時首先調用fragment.setTimerValue(value)

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

然后在OnCreateView()方法的末尾執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM