[英]How to get tab click in Android?
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. 我如何基于选定的选项卡将变量值设置为true或false。 But I dont know how to get the tab click for this tab. 但我不知道如何让该标签单击。 Can you please help me on this. 你能帮我解决这个问题吗?
The code : 编码 :
tabHost.setup(); 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().getDrawable(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. 将onTabchangedListener添加到选项卡主机,然后使用selectedTab Value进行管理。 selectedTab value will be = 0 for first tab and rest so on 对于第一个选项卡,selectedTab值将= 0 =,依此类推
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
int selectedTab = tabHost.getCurrentTab() // selected
}
});
Hope it helps :) 希望能帮助到你 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.