tabhost/tabspec 是否有类似 onclick/onfocus 的东西?

发布于 2025-01-04 00:01:37 字数 1531 浏览 3 评论 0原文

我有一个完全工作的 tabhost/tabspec 应用程序(第一个工作的自制应用程序:D)请参阅下面的代码。 现在,如下:当按下选项卡 NORM 时,我想运行几行代码。 tabhost/tabspec 是否有类似 onclick 的东西?任何帮助表示赞赏

public class AndroidTabLayoutActivity extends TabActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabHost = getTabHost();

    // Tab for Dag
    TabSpec dagspec = tabHost.newTabSpec("Dag");
    dagspec.setIndicator("DagRooster", getResources().getDrawable(R.drawable.icon_dag_tab));
    Intent dagIntent = new Intent(this, DagActivity.class);
    dagspec.setContent(dagIntent);


    // Tab for Norm
    TabSpec normspec = tabHost.newTabSpec("Norm");
    // setting Title and Icon for the Tab
    normspec.setIndicator("Normaal", getResources().getDrawable(R.drawable.icon_norm_tab));
    Intent normIntent = new Intent(this, NormActivity.class);
    normspec.setContent(normIntent);


    // Tab for Instel
    TabSpec instelspec = tabHost.newTabSpec("Instel");
    instelspec.setIndicator("Info", getResources().getDrawable(R.drawable.icon_setting_tab));
    Intent instelIntent = new Intent(this, InstelActivity.class);
     instelspec.setContent(instelIntent);


    // Adding all TabSpec to TabHost
    tabHost.addTab(dagspec); // Adding photos tab
    tabHost.addTab(normspec); // Adding songs tab
    tabHost.addTab(instelspec); // Adding videos tab
    tabHost.setCurrentTab(2);

        }

}

I've a fully working tabhost/tabspec app (first working self-made app ever :D) see code beneath.
Now, the following: when tab NORM is pressed I want to run a few lines of code. Is there something like an onclick for the tabhost/tabspec. Any help appreciated

public class AndroidTabLayoutActivity extends TabActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabHost = getTabHost();

    // Tab for Dag
    TabSpec dagspec = tabHost.newTabSpec("Dag");
    dagspec.setIndicator("DagRooster", getResources().getDrawable(R.drawable.icon_dag_tab));
    Intent dagIntent = new Intent(this, DagActivity.class);
    dagspec.setContent(dagIntent);


    // Tab for Norm
    TabSpec normspec = tabHost.newTabSpec("Norm");
    // setting Title and Icon for the Tab
    normspec.setIndicator("Normaal", getResources().getDrawable(R.drawable.icon_norm_tab));
    Intent normIntent = new Intent(this, NormActivity.class);
    normspec.setContent(normIntent);


    // Tab for Instel
    TabSpec instelspec = tabHost.newTabSpec("Instel");
    instelspec.setIndicator("Info", getResources().getDrawable(R.drawable.icon_setting_tab));
    Intent instelIntent = new Intent(this, InstelActivity.class);
     instelspec.setContent(instelIntent);


    // Adding all TabSpec to TabHost
    tabHost.addTab(dagspec); // Adding photos tab
    tabHost.addTab(normspec); // Adding songs tab
    tabHost.addTab(instelspec); // Adding videos tab
    tabHost.setCurrentTab(2);

        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抠脚大汉 2025-01-11 00:01:37

您只需将 TabHost 添加到 OnTabChangeListener() 中,如下所示:

// Exit Application when press Exit tab
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {     
        @Override
        public void onTabChanged(String arg0) {
            if (getTabHost().getCurrentTabTag().equals("Exit")){
                finish();                   
            }
        }
    });

You just need to add TabHost to OnTabChangeListener() like this:

// Exit Application when press Exit tab
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {     
        @Override
        public void onTabChanged(String arg0) {
            if (getTabHost().getCurrentTabTag().equals("Exit")){
                finish();                   
            }
        }
    });
身边 2025-01-11 00:01:37

我想您可以使用

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
   @Override
  public void onTabChanged(String arg0) {
   Log.i("param1", "param2" + tabHost.getCurrentTab());
  }     
       });  

或者

您可以使用此SO讨论中讨论的方法。

I guess you can use

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
   @Override
  public void onTabChanged(String arg0) {
   Log.i("param1", "param2" + tabHost.getCurrentTab());
  }     
       });  

or

You may use the approach discussed in this SO discussion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文