android - 如何在单击按钮时滑动标签

标签 android button tabs swipe

如何在按钮点击事件中滑动标签页?

即,如果按下计算按钮,则用户将被移动到选项卡式 Activity 上的另一个页面,为他们提供结果。

public class MainActivity extends ActionBarActivity implements   
android.support.v7.app.ActionBar.TabListener {

private ViewPager mViewPager;

//TODO: Find alternatives to deprecated methods
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
    }
}

public void onClick(View view){
    EditText weight_entry = (EditText) findViewById(R.id.weight_entry);
    EditText height_entry = (EditText) findViewById(R.id.height_entry);
    String weight = weight_entry.getText().toString();
    String height = height_entry.getText().toString();
    TextView calc_label = (TextView) findViewById(R.id.calc_label);

    if (weight.matches("")|| height.matches("")){
        return;
    } else {
        float t;
        float w = Float.parseFloat(weight);
        float h = Float.parseFloat(height);

        //TODO: check this
        t = (float)((double)Math.round(10D * (double)(w / (h * h))) / 10D);

        calc_label.setText("Your BMI is: " + Float.toString(t));
    }


    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(weight_entry.getWindowToken(), 0);
    imm.hideSoftInputFromWindow(height_entry.getWindowToken(), 0);
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new InputFragment();
            case 1:
                return new ResultFragment();
            case 2:
                return new HistoryFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

最佳答案

你能发布一些你的代码吗?

无论如何,尝试做:

mTabHost.setCurrentTab(idx);

更新:

好吧,显然你有一个 ViewPager,而不是一个 Tab ;)

在你的 onClick 上试试这个 mViewPager.setCurrentItem(idx);

得到了idx,你要展示的页面的索引。我猜你的情况是 1。

关于android - 如何在单击按钮时滑动标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27741373/

相关文章:

java - Android Lollipop Material Design 溢出菜单图标颜色

android - 仅 Lollipop 崩溃 "Failed resolution of: Landroid/support/v7/appcompat/R$styleable"

python - 如何在 turtle 中创建按钮?

java - Android按钮闪烁动画与不同的图像

vim - 通过在 Vim 中键入其名称来切换到特定选项卡?

android - 在手机(android/ios)上生成没有跟踪器的增强现实对象

android - 如何将sdcard中图片的经纬度获取到我的应用程序中?

javascript - 使用 javascript 和 ajax 将 bootstrap 按钮变成链接

html - 如何将按钮链接到另一个页面上带有 div 内容的选项卡?

javascript - 如何防止 Bootstrap 选项卡干扰 angularjs 路由?