单击选项卡上的启动活动

时间:2013-08-21 04:11:04

标签: android tabs

我有3个标签,在点击时显示不同的内容。我希望他们在点击时打开某些活动。这些活动只是需要显示的数据库报告。我使用Web视图并在API级别为11的模拟器上正常工作,但在我的设备上发出了Web page not found错误,这是API级别10.此外,选项卡仅在单击其他选项卡并返回第一个后显示数据。 有没有办法可以直接启动活动而不是使用Web视图?

感谢任何帮助。

XML

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   <TabHost 
       android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearLayout1"
        android:orientation="vertical"
        >
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" ></TabWidget>
    <FrameLayout 
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tabs1"
            android:orientation="vertical"
            >
            <WebView
                android:id="@+id/txtGetFuelInfo"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Get info from Database" />
                <TextView
android:id="@+id/txtGetFuel"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
        </LinearLayout>
        <LinearLayout 
              android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tabs2"
            android:orientation="vertical"
            >

          <WebView
                android:id="@+id/txtMainGetInfo"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Get info from Database"/>
<TextView
                android:id="@+id/txtGetMain"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>


        </LinearLayout>

        <LinearLayout 
             android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tabs3"
            android:orientation="vertical"
            >
            <WebView
                android:id="@+id/txtSpareGetInfo"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Get info from Database"/>
<TextView
android:id="@+id/txtGetspare"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>

        </LinearLayout>
    </FrameLayout>
    </LinearLayout>
   </TabHost>
</LinearLayout>

班级

public class DetailsTabs extends Activity
{
    TabSpec tspec;
    FuelStoredInfo  fuelInfo;
    WebView fuelView,mainView,sparesView;
    TextView tvFuel, tvMain, tvSpares;
    WebSettings fuelSetting, mainSetting, spareSetting;
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailstabs);

        fuelView = (WebView)findViewById(R.id.txtGetFuelInfo);
        mainView = (WebView)findViewById(R.id.txtMainGetInfo);
        sparesView = (WebView)findViewById(R.id.txtSpareGetInfo);

        //tvFuel = (TextView)findViewById(R.id.totalFuelId);
        //tvMain = (TextView)findViewById(R.id.totalmainId);
        //tvSpares = (TextView)findViewById(R.id.totalspareId);

        fuelInfo = new FuelStoredInfo(this);
        fuelInfo.open();


        TabHost th = (TabHost)findViewById(R.id.tabhost);
        th.setup();

        // Fuel Expense tab
        tspec = th.newTabSpec("Tag 1");

        tspec.setContent(R.id.tabs1);
        tspec.setIndicator("Fuel"+"\n"+"Expense");
        String Data = fuelInfo.getData();
           double fuelTotal  =fuelInfo.getTotalFuel();
        fuelSetting = fuelView.getSettings();
        fuelSetting.setDefaultFontSize(10);
        //tvFuel.setText("Total fuel expense : Rs. "+ String.valueOf(fuelTotal));
        fuelView.loadData(Data,"text/html",null);
        th.addTab(tspec);

        //Maintenance Expense tab
        tspec = th.newTabSpec("Tag 2");
        tspec.setContent(R.id.tabs2);
        tspec.setIndicator("Maintenance"+"\n"+"Expense");
        mainSetting = mainView.getSettings();
        mainSetting.setDefaultFontSize(10);
        String mainData = fuelInfo.getMainData();
        double mainTotal  =fuelInfo.getTotalMain();
        tvMain.setText("Total maintenance expense : Rs. "+ String.valueOf(mainTotal));
        mainView.loadData(mainData,"text/html",null);

        th.addTab(tspec);

        //Spares Expense tab
        tspec = th.newTabSpec("Tag 3");

        tspec.setContent(R.id.tabs3);
        tspec.setIndicator("Spares"+"\n"+"Expense");
        spareSetting = sparesView.getSettings();
        spareSetting.setDefaultFontSize(10);
        String sparesData= fuelInfo.getSpareData();
        double spareTotal = fuelInfo.getTotalSpares();
        tvSpares.setText("Total spares expense : Rs."+ String.valueOf(spareTotal));
        sparesView.loadData(sparesData,"text/html",null);

        th.addTab(tspec);


        fuelInfo.close();



    }

}

2 个答案:

答案 0 :(得分:0)

不推荐使用TabHost,为什么不使用ActionBar?

任何方式..我认为你应该使用像这样的TabHost

Resources ressources = getResources(); 
    TabHost tabHost = getTabHost(); 

    // Android tab
    Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
    TabSpec tabSpecAndroid = tabHost
      .newTabSpec("Android")
      .setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
      .setContent(intentAndroid);

这个TabHost Example

有一个很好的教程

答案 1 :(得分:0)

试试这个

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

    TabSpec Spec = null;
    Spec = tabHost.newTabSpec("home");
    Spec.setIndicator("Home", getResources().getDrawable(R.drawable.ic_tab_home));
    Spec.setContent(new Intent(this, MainList.class));
    tabHost.addTab(Spec);

    Spec = tabHost.newTabSpec("recent");
    Spec.setIndicator("Recent", getResources().getDrawable(R.drawable.ic_tab_recent));
    Spec.setContent(new Intent(this, Recent.class));
    tabHost.addTab(Spec);

    Spec = tabHost.newTabSpec("setting");
    Spec.setIndicator("Setting", getResources().getDrawable(R.drawable.ic_tab_setting));
    Spec.setContent(new Intent(this, Setting.class));
    tabHost.addTab(Spec);

    tabHost.getTabWidget().setCurrentTab(0);