簡體   English   中英

如何確定用戶是否單擊了 TabHost 內的選項卡?

[英]How to determine if a user clicked on a tab inside of a TabHost?

我有一個帶有兩個選項卡的 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);

我認為您想使用 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

每次有人切換標簽時,您都可以有效地得到通知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM