制表活动的Andr​​oid设置默认选项卡 [英] android setting default tab in tab-activity

查看:142
本文介绍了制表活动的Andr​​oid设置默认选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

安卓TabActivity推出与之前的设置序列中添加的第一个选项卡关联的FragmentActivity tabhost.setCurretnTab(4);

  @覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.tab_main);
    尝试
    {
        DataSource.ObjContext = this.getApplicationContext();
        DataSource.ObjTabBarActivity =这一点;
        DataSource.ObjShared preferences = this.getShared preferences(com。示例,Context.MODE_PRIVATE);
        如果(NetworkStat)
        {
            新LocationUpdates(本);            this.setTabs();        }
        其他
        {
            Log.d(在TabBarActivity,网络故障);
            Toast.makeText(this.getApplicationContext(),网络故障,Toast.LENGTH_SHORT).show();
        }
    }
   赶上(异常前)
    {    }
}私人无效setTabs()
{
    addTab(俱乐部,R.drawable.tab_clubs,FragmentStackClubsActivity.class);
    addTab(事件,R.drawable.tab_events,FragmentStackEventsActivity.class);
    addTab(速度,R.drawable.tab_rate,FragmentStackRateActivity.class);
    addTab(忠诚,R.drawable.tab_loyalty,FragmentStackLoyaltyActivity.class);
    addTab(设置,R.drawable.tab_settings,FragmentStackSettingsActivity.class);
    如果(DataSource.ObjShared preferences.getString(DataSource.LOGIN_TAG,假)。equalsIgnoreCase(假))
    {        getTabHost()setCurrentTab(4)。
        DataSource.disableTabBar();
    }
    其他
    {    }}
私人无效addTab(字符串labelId,INT drawableId,类<> C)
{
    TabHost tabHost = getTabHost();
    意向意图=新意图(这一点,C);
    TabHost.TabSpec规格= tabHost.newTabSpec(标签+ labelId);    查看tabIndicator = LayoutInflater.from(本).inflate(R.layout.tab_indicator,getTabWidget(),FALSE);
    TextView的标题=(TextView中)tabIndicator.findViewById(R.id.title);
    title.setText(labelId);
    ImageView的图标=(ImageView的)tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);    spec.setIndicator(tabIndicator);
    spec.setContent(意向);
    tabHost.addTab(规范);
}

但问题是,它最初启动的第一个选项卡,然后切换到第5片这样一个线程会从第一个标签开始,这就是我不想也就是说,如果用户没有登录我想重定向用户登录(设置)选项卡。
这方面的任何帮助是非常AP preciated ......


解决方案

 公共无效setCurrentTab(INT指数)
公共无效setCurrentTabByTag(字符串标签)

您可以喜欢这个

 如果(isNotLogin){
     tabHost.setCurrentTabByTag(设置);
}

addTab后()

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

android TabActivity launches the FragmentActivity associated with the first tab added in the sequence before setting tabhost.setCurretnTab(4);

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

but the problem is that it starts the first tab initially and then switches to 5th tab in this way a thread gets started from the first tab and that's what i don't want i.e if the user is not logged in i wanna redirect the user to login(settings) tab. any help in this regard is highly appreciated......

解决方案

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

you can like this

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

after addTab()

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

这篇关于制表活动的Andr​​oid设置默认选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆