1

我正在创建一个简单的应用程序,它总共有 3 个活动

  1. 登录 - 拥有用户名、密码字段以及登录和注册按钮
  2. 注册 - 具有各种表单字段和提交按钮
  3. 留言- 登录成功后进入本次活动

我使用 tabhost,它有 3 个选项卡,定义了上述 3 个活动。单击选项卡时,我可以在各个活动之间切换,但是当我使用表单上的按钮进行切换时,我看不到选项卡。我想在屏幕上显示标签,无论是从标签还是按钮切换。是否有任何概念,例如维护选项卡布局并且仅更改框架中的活动...我听说过 tabhost 组但无法使用它...有可能吗?保护你

我的标签主机代码是

public class TabActivity extends android.app.TabActivity
{
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);

      /** TabHost will have Tabs */ 
    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); 

    /** TabSpec used to create a new tab.  
     * By using TabSpec only we can able to setContent to the tab.  
     * By using TabSpec setIndicator() we can set name to tab. */

    /** tid1 is firstTabSpec Id. Its used to access outside. */ 
    TabSpec signinTab = tabHost.newTabSpec("tid1");  
    TabSpec signupTab = tabHost.newTabSpec("tid2"); 
    TabSpec messageTab = tabHost.newTabSpec("tid3");

    /** TabSpec setIndicator() is used to set name for the tab. */ 
    /** TabSpec setContent() is used to set content for a particular tab. */ 
    signinTab.setIndicator("Sign In").setContent(new Intent(getApplicationContext(),SignIn.class));  
    signupTab.setIndicator("Sign Up").setContent(new Intent(getApplicationContext(),MainActivity.class));
    messageTab.setIndicator("Message").setContent(new Intent(getApplicationContext(),MessageDisplay.class));

    /** Add tabSpec to the TabHost to display. */ 
    tabHost.addTab(signinTab);  
    tabHost.addTab(signupTab); 
    tabHost.addTab(messageTab);//set Windows tab as default (zero based)

  // tabHost.setCurrentTab(2);
}

}
4

1 回答 1

1

Java 代码:

public class TabActivity extends android.app.TabActivity
{
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);

      /** TabHost will have Tabs */ 
    TabHost tabHost = getTabHost();

    /** TabSpec used to create a new tab.  
     * By using TabSpec only we can able to setContent to the tab.  
     * By using TabSpec setIndicator() we can set name to tab. */

    /** tid1 is firstTabSpec Id. Its used to access outside. */ 
    TabSpec signinTab = tabHost.newTabSpec("tid1");  
    TabSpec signupTab = tabHost.newTabSpec("tid2"); 
    TabSpec messageTab = tabHost.newTabSpec("tid3");

    /** TabSpec setIndicator() is used to set name for the tab. */ 
    /** TabSpec setContent() is used to set content for a particular tab. */ 
    signinTab.setIndicator("Sign In").setContent(new Intent(getApplicationContext(),SignIn.class));  
    signupTab.setIndicator("Sign Up").setContent(new Intent(getApplicationContext(),MainActivity.class));
    messageTab.setIndicator("Message").setContent(new Intent(getApplicationContext(),MessageDisplay.class));

    /** Add tabSpec to the TabHost to display. */ 
    tabHost.addTab(signinTab);  
    tabHost.addTab(signupTab); 
    tabHost.addTab(messageTab);//set Windows tab as default (zero based)

  // tabHost.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="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>


    </LinearLayout>
</TabHost>
于 2012-10-23T09:58:56.740 回答