Tab主机-setCurrentTab(x)适用于Tab,但不适用于内容[Android]
我已经创建了tab应用程序。。现在我正在玩屏幕旋转。我试图设置Tab主机-setCurrentTab(x)适用于Tab,但不适用于内容[Android],android,tabs,android-tabhost,Android,Tabs,Android Tabhost,我已经创建了tab应用程序。。现在我正在玩屏幕旋转。我试图设置tabHost.getTabWidget().setCurrentTab(1),它应该显示第二个选项卡(第一个是0)。关键是第二个选项卡显示为选中,但显示的内容来自第一个选项卡。。。我怎样才能解决这个问题 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(W
tabHost.getTabWidget().setCurrentTab(1)
,它应该显示第二个选项卡(第一个是0)。关键是第二个选项卡显示为选中,但显示的内容来自第一个选项卡。。。我怎样才能解决这个问题
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
myCommunicator = new Communicator();
dbAdapter = new ToDoDBAdapter(this);
if (getLastNonConfigurationInstance() != null)
{
CurrentTab = (Integer)getLastNonConfigurationInstance();
createView();
}
else
{
BuildDialog = ProgressDialog.show(this, "Loading", "Updating data...", true, false);
BuildDialog.show();
new LoadChannels().execute();
}
}
private void createView()
{
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setOnTabChangedListener(this);
Intent intent;
TabSpec spec;
intent = new Intent().setClass(this, Channels.class);
// TAB 1
spec = tabHost.newTabSpec("kanali").setIndicator("Kanali",getResources().getDrawable(R.drawable.menu_channels)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Currently.class);
// TAB 2
spec = tabHost.newTabSpec("trenutno").setIndicator("Trenutno",getResources().getDrawable(R.drawable.menu_current)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Reminders.class);
// TAB 3
spec = tabHost.newTabSpec("opomniki").setIndicator("Opomniki",getResources().getDrawable(R.drawable.menu_reminder)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, About.class);
// TAB 4
spec = tabHost.newTabSpec("oprogramu").setIndicator("O programu",getResources().getDrawable(R.drawable.menu_about)).setContent(intent);
tabHost.addTab(spec);
tabHost.setBackgroundColor(Color.WHITE);
tabHost.setCurrentTab(1); // Should always set content to second
}
@凌驾
公共对象OnRetainOnConfiguration实例()
{
返回当前选项卡;
}
为什么要调用
getTabWidget()
?您应该在tabHost
本身上使用setCurrentTab()
这里很好用
tabHost.setCurrentTab(1);
在尝试设置currentTab之前,是否在调试器中检查了TabHost以确保所有选项卡都已成功添加?我遇到了类似的问题,发现如果您尝试引用未成功添加的选项卡范围之外的索引,则TabHost.setCurrentTab仍设置为-1。Android文档中没有记录此行为。使用setCurrentTabByTag(字符串标记);根据您提到的标签,它将是默认的标签,希望它有帮助,工作正常(它目前正在为代码工作!!)
以前试过。。。但与tabHost.setCurrentTab(1)或tanHost.getTabWidget()setCurrentTab(1)的结果相同。@M.V.Ehhh。。。我在我自己的应用程序中使用上述代码进行刷卡(监听事件,如果刷卡有效,则更改选项卡),并且效果非常好。如果它对你不起作用,那么你的问题就在代码的其他地方。我添加了代码,处理选项卡主机。。。希望这将有助于解决一个问题。坦克!有什么特别的原因使整个设计与Android教程有很大的不同吗?我的非常相似,特别是
tabHost=getTabHost()代码>调用可能是必需的。
tabHost.setCurrentTab(1);
private static final String A ="Kanali"; //required defualt tab name
.
.
.
tabHost.setCurrentTabByTag (A);