如何在单击按钮时从一个片段移动到另一个片段 [英] how to move from one fragment to another fragment on button click

查看:79
本文介绍了如何在单击按钮时从一个片段移动到另一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SherlockFragments库作为滑动菜单. 我单击项目片段作为活动打开,但它是一个片段.现在我是片段的新手.我不知道如何从一个片段移动到另一个片段. .但是在片段中我不怎么移动到另一个片段.fragmentA中有一个按钮.当我单击此按钮时,它将移至片段B. 通过谷歌搜索,我知道它具有不同的周期,但是无论如何,当我单击按钮时,我会得到吐司味精 这是以下代码

I am using the SherlockFragments library for the sliding menu.I have list of items as menu when I click on item fragment get opened as an activity but it is a fragment.Now I am new to fragments.i don't know how to move from one fragment to another fragment.As in activity, we have intent to move to another activity.but in fragment I don't how to move to another fragment.I have a button in fragmentA.when I click on this button it moves to fragment B. By googling I came to know that it has different cycles but anyhow I get toast msg when I click button here is following code

public class Fragment2 extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.actionnetworklogin, container, false);
        Button login = (Button)view.findViewById(R.id.login);

        login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(v.getContext().getApplicationContext(),"login clicked", 5000).show();
            }
        });
        return view;
    }   
}

有人可以告诉我如何将一个片段移动到另一个片段吗?

Can someone please tell me how can I move one fragment to another fragment?

还有其他方法可以使用活动而不是片段吗?

Is there any other method that I can use activities instead of fragments?

我已经为所有活动和Java文件编写了代码,但是我不知道滑动菜单已经碎片化,现在我必须编写所有代码碎片.

I have written code for all activities and java files but I don't know that sliding menu has fragmented and now I have to write all the code fragments.

推荐答案

很简单只有三行代码...

It's simple Only three line code...

Fragment fragment = new SalesFragment();

FragmentManager fragmentManager = getFragmentManager();

fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

这篇关于如何在单击按钮时从一个片段移动到另一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆