Android,如何始终将第一个选项卡设置为默认选项卡
public class SoapBox extends TabActivity{
private TabHost tabHost;
private void setupTabhost()
{
tabHost=(TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
TabHost tabHost=getTabHost();
setupTabhost();
tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_space);
Intent intent1=new Intent().setClass(this, MySoapBox.class);
setupTab(new TextView(this), "My SoapBox", intent1);
Intent intent2=new Intent().setClass(this, FriendsSoapBox.class);
setupTab(new TextView(this),"Friends SoapBox", intent2);
Intent intent3=new Intent().setClass(this, Recommendations.class);
setupTab(new TextView(this),"Recommendations", intent3);
tabHost.setCurrentTab(0);
}
private void setupTab(final View view, final String tag, Intent intent)
{
View tabView=createTabView(tabHost.getContext(), tag);
TabSpec tabSpec=tabHost.newTabSpec(tag).setIndicator(tabView).setContent(intent);
tabHost.addTab(tabSpec);
}
private static View createTabView(final Context context,final String text)
{
View view= LayoutInflater.from(context).inflate(R.layout.tabs_bg,null);
TextView textView=(TextView)view.findViewById(R.id.tabsText);
textView.setText(text);
return view;
}
}
在这里,我在该选项卡按钮内还有一个选项卡小部件,我实现了一个嵌套选项卡小部件,当我将一个选项卡切换到另一个选项卡并返回到上一个选项卡时,我没有将焦点放在第一个选项卡上。
当我再次选择父选项卡时,嵌套选项卡的焦点应始终位于第一个选项卡上。 请指导我,我的代码哪里出错了? 提前致谢。
public class SoapBox extends TabActivity{
private TabHost tabHost;
private void setupTabhost()
{
tabHost=(TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
TabHost tabHost=getTabHost();
setupTabhost();
tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_space);
Intent intent1=new Intent().setClass(this, MySoapBox.class);
setupTab(new TextView(this), "My SoapBox", intent1);
Intent intent2=new Intent().setClass(this, FriendsSoapBox.class);
setupTab(new TextView(this),"Friends SoapBox", intent2);
Intent intent3=new Intent().setClass(this, Recommendations.class);
setupTab(new TextView(this),"Recommendations", intent3);
tabHost.setCurrentTab(0);
}
private void setupTab(final View view, final String tag, Intent intent)
{
View tabView=createTabView(tabHost.getContext(), tag);
TabSpec tabSpec=tabHost.newTabSpec(tag).setIndicator(tabView).setContent(intent);
tabHost.addTab(tabSpec);
}
private static View createTabView(final Context context,final String text)
{
View view= LayoutInflater.from(context).inflate(R.layout.tabs_bg,null);
TextView textView=(TextView)view.findViewById(R.id.tabsText);
textView.setText(text);
return view;
}
}
Here I have one more Tab widget inside that Tab button I implemented one nested Tab widget, when I switching one tab to another and come back to previous tab then I am didn't get focus on first tab.
The focus of nested tab should always on first tab when I again select the parent tab.
Please guide me, where am I doing mistake in my code?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setCurrentTab();
属性决定默认选项卡。如果您使用
tabHost.setCurrentTab(n);
那么第 n 个选项卡将是默认选项卡。setCurrentTab();
property decides the default tab.If you use
tabHost.setCurrentTab(n);
then n th tab will be the default tab.