0

我使用 TabHost 并且我有 2 个带有 2 个活动的选项卡我想将第二个选项卡设置为默认选项卡但是当 tabhost 启动时他加载第一个 tab1 和 tab2 之前这是错误的,因为在这 2 个活动中我从 web 服务加载数据!我的问题是如何在不加载第一个选项卡的情况下设置当前选项卡的第二个选项卡!

我的标签主机代码:

public class TabsHandler extends TabActivity    {
private static TabHost tabHost;
Intent intent;
    private void setupTabHost() {
    tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup(this.getLocalActivityManager());
    tabHost.setCurrentTab(1);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if(!isOnline())
                Utils.DialogNoInternet(this);
            String authToken = getIntent().getExtras().getString("token");
            Long role = getIntent().getExtras().getLong("role");
            String EventIdNotification =getIntent().getExtras().getString("EventIdNotification");
            setContentView(R.layout.event_list_tabs);    

            setupTabHost();
            Resources res = getResources();
            tabHost.getTabWidget().setClipChildren(false);


            setupTab(new TextView(this), res.getString(R.string.tab1_List),new Intent().setClass(this, EventListActivity.class).putExtra("authToken", authToken).putExtra("role", role).putExtra("EventIdNotification", EventIdNotification));
            setupTab(new TextView(this), res.getString(R.string.tab2_List),new Intent().setClass(this, PastEventListActivity.class).putExtra("authToken", authToken).putExtra("role", role).putExtra("isPast", "true"));
            tabHost.setCurrentTab(1);



    }
    public void switchTab(int tab){
            tabHost.setCurrentTab(tab);
    }
    public boolean isOnline() {
        ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }
    private void setupTab(final View view, final String tag,Intent intent) {
            View tabview = createTabView(tabHost.getContext(), tag);
            TabSpec setContent = tabHost.newTabSpec(tag);
            setContent.setIndicator(tabview);
            setContent.setContent(intent);
            tabHost.addTab(setContent);
    }

    private static View createTabView(final Context context, final String text) {

            View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
            TextView tv = (TextView) view.findViewById(R.id.tabsText);
            tv.setText(text);
            return view;
    }
4

2 回答 2

1

替换tabHost.setCurrentTab(1);tabHost.setCurrentTab(2);会将第二个选项卡设置为默认选项卡。

于 2012-06-06T10:05:26.980 回答
0
public class TabsHandler extends TabActivity    {
private static TabHost tabHost;
   Intent intent;
private void setupTabHost() {
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this.getLocalActivityManager());
tabHost.setCurrentTab(2);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!isOnline())
            Utils.DialogNoInternet(this);
        String authToken = getIntent().getExtras().getString("token");
        Long role = getIntent().getExtras().getLong("role");
        String EventIdNotification =getIntent().getExtras().getString("EventIdNotification");
        setContentView(R.layout.event_list_tabs);    

        setupTabHost();
        Resources res = getResources();
        tabHost.getTabWidget().setClipChildren(false);


        setupTab(new TextView(this), res.getString(R.string.tab1_List),new Intent().setClass(this, EventListActivity.class).putExtra("authToken", authToken).putExtra("role", role).putExtra("EventIdNotification", EventIdNotification));
        setupTab(new TextView(this), res.getString(R.string.tab2_List),new Intent().setClass(this, PastEventListActivity.class).putExtra("authToken", authToken).putExtra("role", role).putExtra("isPast", "true"));
        tabHost.setCurrentTab(2);



}
public void switchTab(int tab){
        tabHost.setCurrentTab(tab);
}
public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}
private void setupTab(final View view, final String tag,Intent intent) {
        View tabview = createTabView(tabHost.getContext(), tag);
        TabSpec setContent = tabHost.newTabSpec(tag);
        setContent.setIndicator(tabview);
        setContent.setContent(intent);
        tabHost.addTab(setContent);
}

private static View createTabView(final Context context, final String text) {

        View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(text);
        return view;
}
于 2012-06-06T10:03:23.317 回答