Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在单击按钮时滑动选项卡_Android_Button_Tabs_Swipe - Fatal编程技术网

Android 如何在单击按钮时滑动选项卡

Android 如何在单击按钮时滑动选项卡,android,button,tabs,swipe,Android,Button,Tabs,Swipe,如何在按钮单击事件上滑动选项卡 i、 e如果按下“计算”按钮,则用户将被移动到选项卡式活动的另一个页面,该页面将给出他们的结果 public class MainActivity extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener { private ViewPager mViewPager; //TODO: Find alternatives to deprecated m

如何在按钮单击事件上滑动选项卡

i、 e如果按下“计算”按钮,则用户将被移动到选项卡式活动的另一个页面,该页面将给出他们的结果

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;
    }
}
public类MainActivity扩展了ActionBarActivity实现
android.support.v7.app.ActionBar.TabListener{
私有视图寻呼机mViewPager;
//TODO:找到不推荐方法的替代方法
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终ActionBar ActionBar=getSupportActionBar();
actionBar.setNavigationMode(actionBar.NAVIGATION\u MODE\u选项卡);
SectionsPagerAdapter mSectionsPagerAdapter=新的SectionsPagerAdapter(getSupportFragmentManager());
mViewPager=(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(新的ViewPager.SimpleOnPageChangeListener(){
@凌驾
已选择页面上的公共无效(内部位置){
actionBar.setSelectedNavigationItem(位置);
}
});
对于(int i=0;i
你能发布一些你的代码吗

无论如何,试着做:

mTabHost.setCurrentTab(idx);

更新:


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

在您的
onClick上尝试此操作
mviewpage.setCurrentItem(idx);


be idx,您要显示的页面的索引。我猜在您的情况下应该是1。

好的,显然您有一个ViewPager,而不是Tab;)试试这个
mViewPager.setCurrentItem(idx);
be idx,您要显示的页面的索引。我猜在您的情况下,您的onClick上应该是
1