【问题标题】:How to determine if a user clicked on a tab inside of a TabHost?如何确定用户是否单击了 TabHost 内的选项卡?
【发布时间】:2011-05-03 21:17:15
【问题描述】:

我有一个带有两个选项卡的 TabHost。我想知道用户何时单击任一选项卡。我怎样才能做到这一点?

Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Main.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("watchlist").setIndicator("Watchlist",res.getDrawable(R.drawable.icon)).setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, ARActivity.class);
        spec = tabHost.newTabSpec("trending").setIndicator("Trending",res.getDrawable(R.drawable.icon)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

【问题讨论】:

  • 您的问题不清楚。当用户单击一个选项卡时,选项卡将发生变化,并且新选项卡内的活动将变为活动状态,因此它已经“知道”其选项卡已被单击。
  • 有没有办法给标签添加一个 onclick 监听器,这样我就可以在那时做一些事情?

标签: java android tabs android-tabhost tabwidget


【解决方案1】:

我认为您想使用 onTabChangeListener (http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html) 并将其传递给 TabHost。见http://developer.android.com/reference/android/widget/TabHost.html#setOnTabChangedListener%28android.widget.TabHost.OnTabChangeListener%29

每次有人切换标签时,您都可以有效地得到通知。

【讨论】:

    猜你喜欢
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多