[問題]fragment裡的button切換另一個fragment?

作者: drsimple (九份天空)   2014-07-17 00:13:20
想請問
要如何在按下fragment中的button後,畫面會切換到另外一個fragment?
我在按下button之後,兩個fragment的畫面就重疊在一起了,請問該怎麼辦?
以下是按下按鈕後的程式
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.log_in, container, false);
Intent intent=new Intent();
intent = new Intent(getActivity(), RegisterFragment.class);
Button toRegistPage = (Button)
rootView.findViewById(R.id.btntToRegistPage);
toRegistPage.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v){
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.RelativeLayout1, new RegisterFragment());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
});
return rootView;
}
我還只是個新手,希望各位高手能幫幫忙,謝謝
作者: tac0wu (在BBS中流浪)   2014-07-17 00:34:00
你原來的Fragment是放在哪個container裡面
作者: drsimple (九份天空)   2014-07-17 00:59:00
本來的fragment是layout.log_in,想切到RegisterFragment.class對應的layout
作者: baobomb (baobomb)   2014-07-17 09:11:00
開一個原始的android project 裡面有NavigationDrawerFragment.class 把它看懂 你就會了

Links booklink

Contact Us: admin [ a t ] ucptt.com