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;
    }
}

在这里,我有一个标签内的按钮,我实现了一个嵌套的标签窗口小部件,当我切换一个标签到另一个,回到前一个标签,然后我没有得到重点在第一个标签。
当我再次选择父选项卡时,嵌套选项卡的焦点应始终位于第一个选项卡上。
请引导我,我的代码哪里出错了?
提前谢谢。

最佳答案

setCurrentTab();属性决定默认选项卡。
如果使用tabHost.setCurrentTab(n);,则第n个选项卡将是默认选项卡。

10-07 19:35
查看更多