开发者

How to get tab click in Android?

开发者 https://www.devze.com 2023-03-14 12:51 出处:网络
I have created two tabs in my Activity using the below code. This is working perfectly fine. When I click on the first tab I see its contents and the other one shows its content when clicked on it.

I have created two tabs in my Activity using the below code. This is working perfectly fine. When I click on the first tab I see its contents and the other one shows its content when clicked on it.

How ever I want to set a variable value to true or false based on the Tab selected. But I dont know how to get the tab click for this tab. Can you please help me on this.

The code :

tabHost.setup();

    TabSpec ts = tabHost.newTabSpec("Tab1");
    ts.setIndicator("", getResources().getDrawable(R.drawable.tab1_content));
    ts.setContent(R.id.tab1Layout);


    tabHost.addTab(ts);
    TabSpec ts1 = tabHost.newTabSpec("Tab2");
    ts1.setIndicator("", getResources().getDrawab开发者_如何学编程le(R.drawable.tab2_content));
    ts1.setContent(R.id.tab2Layout);
    tabHost.addTab(ts1);


Add onTabchangedListener to the tabhost and using selectedTab Value manage whatsoever you want to manage. selectedTab value will be = 0 for first tab and rest so on

 tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
    int selectedTab = tabHost.getCurrentTab() // selected
 }
 });

Hope it helps :)

0

精彩评论

暂无评论...
验证码 换一张
取 消