Android选项卡更换为tab3,但内容与tab1相同

时间:2011-10-17 10:05:59

标签: android sqlite

我想在加载时加载tab3但是它会加载带有tab1内容​​的tab3。以下是我为3个选项卡加载的代码,我希望tab3在第一个加载。

package sh.mkt;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class thirdtab extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec thiredTabSpec = tabHost.newTabSpec("tid3");

firstTabSpec.setIndicator("Buying Cost").setContent(new Intent(this,tab1.class));
secondTabSpec.setIndicator("Selling Cost").setContent(new Intent(this,tab2.class));
thiredTabSpec.setIndicator("Portfolio").setContent(new Intent(this,tab3.class));

tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thiredTabSpec);
tabHost.getTabWidget().setCurrentTab(2);
}
}

XML

<?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@android:id/tabhost"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent">  
        <LinearLayout  
            android:orientation="vertical"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:padding="5dp">  
            <TabWidget  
                android:id="@android:id/tabs"  
                android:layout_width="match_parent" android:layout_height="wrap_content"
                android:layout_gravity="center" 
                android:gravity="center" 
                android:orientation="horizontal"
                android:layout_alignParentBottom="true"/>  
            <FrameLayout  
                android:id="@android:id/tabcontent"  
                android:layout_width="fill_parent"  
                android:layout_height="fill_parent"  
                android:padding="5dp"/>  
        </LinearLayout>  
    </TabHost>  

2 个答案:

答案 0 :(得分:1)

在所有三个中你都设置了“tid1”问题可能就在那里.... 您可以参考this ...

  

出于试用原因如果你保持所有类相同而不是看看他们的XML你可能忘记更改它的setcontentview()。

完美的解决方案:

只是使用:

tabHost.setCurrentTab(2);

而不是你的(tabHost.getTabWidget()。setCurrentTab(2);)

cheeers !!!!!

答案 1 :(得分:1)

您可能正在制作像this example这样的演示,然后我怀疑您是否编写了3个不同的java文件以显示在每个选项卡上。请仔细阅读链接。