簡體   English   中英

在特定選項卡中啟動Android App

[英]Start Android App in a specific tab

問題是:我有一個帶有4個選項卡的TabHost (請參見下面的代碼),並且在MainMenuActivity類中有一個Button Button是使用OnClickListener設置的,如果單擊它,則希望它轉到第二個選項卡。 我已經嘗試過使用setCurrentTab(1)但這只是弄亂了項目。 我能做什么?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    setTabs() ; 
}
private void setTabs()
{
    addTab("Home", R.drawable.tab_home, MainMenuActivity.class);
    addTab("Calculate", R.drawable.tab_search, SpinnerClass.class);

    addTab("Search", R.drawable.tab_home, ScrollView1.class);
    addTab("Premium", R.drawable.tab_search, ScrollView2.class);

}

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

}    

tabHost.setCurrentTab(index)是正確的方法。 使用時有什么問題?

“ setCurrentTab(int)打開默認顯示的選項卡,由選項卡的索引位置指定。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM