如何在 Android 中使用 buttonclick 以编程方式切换选项卡 [英] How to programmatically switch tabs using buttonclick in Android

查看:39
本文介绍了如何在 Android 中使用 buttonclick 以编程方式切换选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此苦苦挣扎了几天.我试图在单击按钮时以编程方式切换选项卡.如果我只是使用选项卡来更改活动,我的程序可以完美运行,但是使用 setCurrentTab 连接 onClick 方法会导致错误.这是行不通的方法.这是一个非常基本和直接的功能,但我没有看到太多文档或人们试图将按钮单击与切换选项卡连接的示例.谢谢.

I have been struggling with this for a few days now. I'm trying to switch tabs programmatically upon a button click. My program works flawlessly if I just use the tabs to change activities, but wiring an onClick method with setCurrentTab results in an error. This is the method that will not work. It's a pretty basic and straightforward function but I haven't seen much documentation or examples of people attempting to wire a buttonclick with switching tabs. Thanks.

ImageButton next = (ImageButton) findViewById(R.id.ButtonAsk);
 next.setOnClickListener(new View.OnClickListener() 
         {         
  public void onClick(View view)  
             {

                TabHost tabHost =  (TabHost) findViewById(android.R.id.tabhost);
              tabHost.setCurrentTab(2);                
             }
         });

查看错误日志的编辑历史.

See the edit history for the error log.

推荐答案

这是一个代码示例,您可以将其放入 onClick() 中.正如马克和凯文所描述的那样.

Here's a code example that you can put inside your onClick(). It's as Mark and Kevin described.

    TabActivity tabs = (TabActivity) getParent();
    tabs.getTabHost().setCurrentTab(2);

我已经多次使用此代码花絮.希望这能澄清.

I've used this code tidbit numerous times. Hope this clarifies.

这篇关于如何在 Android 中使用 buttonclick 以编程方式切换选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆