tab-activity中的android设置默认选项卡

时间:2013-05-14 11:04:16

标签: android tabbar tabactivity

android TabActivity在设置tabhost.setCurretnTab(4)之前启动与序列中添加的第一个选项卡关联的FragmentActivity;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_main);
    try
    { 
        DataSource.ObjContext = this.getApplicationContext();
        DataSource.ObjTabBarActivity = this;
        DataSource.ObjSharedPreferences = this.getSharedPreferences("com.example", Context.MODE_PRIVATE);
        if(NetworkStat)
        {
            new LocationUpdates(this);

            this.setTabs();

        }
        else
        {
            Log.d("in TabBarActivity", "Network failure");
            Toast.makeText(this.getApplicationContext(), "Network failure", Toast.LENGTH_SHORT).show();
        }
    }
   catch(Exception ex)
    { 

    }


}

private void setTabs()
{
    addTab("Clubs", R.drawable.tab_clubs,  FragmentStackClubsActivity.class);
    addTab("Events", R.drawable.tab_events, FragmentStackEventsActivity.class);
    addTab("Rate", R.drawable.tab_rate, FragmentStackRateActivity.class);
    addTab("Loyalty", R.drawable.tab_loyalty, FragmentStackLoyaltyActivity.class);
    addTab("Setting", R.drawable.tab_settings, FragmentStackSettingsActivity.class);
    if(DataSource.ObjSharedPreferences.getString(DataSource.LOGIN_TAG, "false").equalsIgnoreCase("false"))
    {

        getTabHost().setCurrentTab(4);
        DataSource.disableTabBar();
    }
    else
    {

    }

}
private void addTab(String labelId, int drawableId, Class<?> c)
{
    TabHost tabHost = getTabHost();
    Intent intent = new Intent(this, c);
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText(labelId);
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);

    spec.setIndicator(tabIndicator);
    spec.setContent(intent);
    tabHost.addTab(spec);
}

但问题是它最初启动第一个标签,然后切换到第五个标签,这样一个线程从第一个标签启动,这就是我不想要的,即如果用户没有登录我想重定向用户登录(设置)选项卡。 在这方面的任何帮助都非常感谢......

1 个答案:

答案 0 :(得分:1)

public void setCurrentTab (int index)
public void setCurrentTabByTag (String tag)

你可以喜欢这个

if (isNotLogin) {
     tabHost.setCurrentTabByTag("Setting");
}

在addTab()

之后

http://developer.android.com/reference/android/widget/TabHost.html#setCurrentTab(int)

相关问题