0

我在操作栏中有一些选项卡,如下面的代码所示。但我想向每个选项卡添加一些按钮和 EditText 等内容。我不知道该怎么做。谁能告诉我应该把这个放在哪里。我会很高兴为您提供任何帮助。提前致谢。

public class MLkeyboardActivity extends FragmentActivity implements
    ActionBar.TabListener {

/**
 * The serialization (saved instance state) Bundle key representing the
 * current tab position.
 */
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mlkeyboard);

    // Set up the action bar to show tabs.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // For each of the sections in the app, add a tab to the action bar.
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section1)
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section2)
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section3)
            .setTabListener(this));
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Restore the previously serialized current tab position.
    if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
        getActionBar().setSelectedNavigationItem(
                savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    // Serialize the current tab position.
    outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
            .getSelectedNavigationIndex());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_mlkeyboard, menu);
    return true;
}


@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, show the tab contents in the
    // container view.

    Fragment fragment = new DummySectionFragment();
    Bundle args = new Bundle();
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,
            tab.getPosition() + 1);
    fragment.setArguments(args);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, fragment).commit();
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        textView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return textView;
    }
}

}

4

3 回答 3

1

但我想向每个选项卡添加一些按钮和 EditText 等内容。我不知道该怎么做。

将它们放入您正在加载的片段中onTabSelected()

或者,修改onTabSelected()以执行其他操作,而不是替换片段,以影响您对 UI 的期望更改。

于 2013-02-07T19:49:07.150 回答
0

你可以参考这个:

http://developer.android.com/reference/android/app/ActionBar.html

此外,您可以在此基础上创建一个 OptionCommand 和 setAction。

于 2013-02-07T19:44:31.753 回答
0

本教程解决了我的问题。我们要做的是创建与每个选项卡相对应的片段(例如 FragmentA、B、C),并使用每个选项卡调用这些片段。代码可在 http://android-er.blogspot 下面的链接中下载。在/2012/06/create-actionbar-in-tab-navigation-mode.html

public class AndroidNavigationTabsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tabA = actionBar.newTab();
    tabA.setText("Tab A");
    tabA.setTabListener(new TabListener<MyFragmentA>(this, "Tag A", MyFragmentA.class));
    actionBar.addTab(tabA);

    Tab tabB = actionBar.newTab();
    tabB.setText("Tab B");
    tabB.setTabListener(new TabListener<MyFragmentB>(this, "Tag B", MyFragmentB.class));
    actionBar.addTab(tabB);

    Tab tabC = actionBar.newTab();
    tabC.setText("Tab C");
    tabC.setTabListener(new TabListener<MyFragmentC>(this, "Tag C", MyFragmentC.class));
    actionBar.addTab(tabC);

    if (savedInstanceState != null) {
        int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
        getActionBar().setSelectedNavigationItem(savedIndex);
    }

}
于 2013-02-08T15:34:52.613 回答