簡體   English   中英

如何更改按鈕單擊時選擇的選項卡?

[英]How to change the selected tab on button click?

我遵循了在這里找到的教程:

在Android中實現片段標簽

並且我可以正常運行,但是我希望能夠通過單擊按鈕(而不只是選擇選項卡)來切換片段。 因此,在FragmentTab1()內部,我添加了一個ImageButton,可以切換片段,然后打開FragmentTab2()

這是OnCreateView()FragmentTab1()內部的代碼:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_form1, container, false);
    btnFragment1 = (ImageButton) rootView.findViewById(R.id.next);

    btnFragment1.setOnClickListener(btnFragmentOnClickListener);

    return rootView;
}

這是我的按鈕的代碼:

Button.OnClickListener btnFragmentOnClickListener
        = new Button.OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Fragment newFragment;

        newFragment = new FragmentTab2();

        // Create new transaction
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }};
}

這可以正常工作以切出片段。

我的問題是 :如何獲取所選標簽,使標簽本身中的文本下方顯示藍線? 任何幫助,將不勝感激!

編輯:我沒有使用TabHost,所以這使解決方案更加棘手,我正在使用ActionBar.TabListener。

添加這兩行在我的按鈕處理程序中成功了:

ActionBar actionBar = (ActionBar)getActivity().getActionBar();
actionBar.setSelectedNavigationItem(1);

暫無
暫無

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

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