【问题标题】:Tabhost android标签主机安卓
【发布时间】:2012-10-23 09:43:58
【问题描述】:

我正在创建一个简单的应用程序,总共有 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);
}

}

【问题讨论】:

  • 但 TabActivity 现在已弃用。
  • 仅在 4.0 及更高版本上已弃用,但超过 50% 的手机仍为 2.3.6 及更低版本。他的代码需要代表它们。
  • 如果我通过 tabhost 进入其他活动,那么没关系,但是当我通过使用按钮而不是 tabhost 进入其他活动时,那么在该活动中如何显示选项卡主机。那么有没有可能
  • 它可以包含在单个活动中。使用注册按钮显示登录活动。当用户点击它时打开注册页面。并根据成功/错误消息显示消息。
  • 如何..对不起,但我不明白你能用代码解释一下吗..

标签: android android-intent android-tabhost


【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2013-03-31
    • 1970-01-01
    • 2012-06-15
    • 2012-02-16
    相关资源
    最近更新 更多