ActionBar选项卡:从片段1到片段2的onclick按钮

时间:2014-11-26 19:39:13

标签: android android-fragments android-actionbar

我是Android开发的新手,需要帮助!

我在教程:http://developer.android.com/guide/topics/ui/actionbar.html#Tabs

的帮助下使用ActionBar实现了导航标签

我的应用程序有3个片段。我在片段1上创建了一个按钮,然后单击该按钮我想调用片段2.任何人都可以告诉我如何实现它?

MenuActivity.java

public class MenuActivity extends Activity {
// Declare Tab Variable
ActionBar.Tab TabPreOp, TabConnect, TabInProcedure, TabPostOp;
Fragment preOpFragmentTab = new PreOpFragmentTab();
Fragment cnCFragmentTab = new CnCFragmentTab();
Fragment inProcedureFragmentTab = new InProcedureFragmentTab();
Fragment postOpFragmentTab = new PostOpFragmentTab();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_activity);

    ActionBar actionBar = getActionBar();

    // Hide Actionbar Icon
    actionBar.setDisplayShowHomeEnabled(false);

    // Hide Actionbar Title
    actionBar.setDisplayShowTitleEnabled(false);

    // Create Actionbar Tabs
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    // Set Tab Icon and Titles
    TabPreOp = actionBar.newTab().setText("Pre-Op");
    TabConnect = actionBar.newTab().setText("Connect & Calibrate");
    TabInProcedure = actionBar.newTab().setText("In-Procedure");
    TabPostOp = actionBar.newTab().setText("Post-Op");



    // Set Tab Listeners
    TabPreOp.setTabListener(new TabListener(preOpFragmentTab));
    TabConnect.setTabListener(new TabListener(cnCFragmentTab));
    TabInProcedure.setTabListener(new TabListener(inProcedureFragmentTab));
    TabPostOp.setTabListener(new TabListener(postOpFragmentTab));

    // Add tabs to actionbar
    actionBar.addTab(TabPreOp);
    actionBar.addTab(TabConnect);
    actionBar.addTab(TabInProcedure);
    actionBar.addTab(TabPostOp);
}

片段1

public class PreOpFragmentTab extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.pre_op_activity, container, false);

    Button button_continue = (Button) rootView.findViewById(R.id.button_continue);
    button_continue.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {


        }
    });
    return rootView;
}

}

我有一个TabListner类。

有人可以给我一些建议怎么做吗?提前谢谢。

Sushant

1 个答案:

答案 0 :(得分:0)

这是片段交易的一个例子:

button_continue.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
    Transaction transaction = getSupportFragmentManager().beginTransaction();
    ExampleFragment fragment = new ExampleFragment();
    transaction.replace(R.id.container, fragment); //This R.id.container should be replaced for the id you have in your activity(If u want show me your xml of your parent Activity)
    transaction.addToBackStack(null); //If you want to add to the stack add this line
    transaction.commit();

    }
});

有关详细信息,请参阅本指南:

http://developer.android.com/guide/components/fragments.html