gpt4 book ai didi

android - tabhost 安卓

转载 作者:行者123 更新时间:2023-11-30 03:58:04 26 4
gpt4 key购买 nike

我正在创建总共有 3 个 Activity 的简单应用程序

  1. 登录 - 具有用户名、密码字段以及登录和注册按钮
  2. 注册 - 具有各种表单字段和提交按钮
  3. 留言-登录成功后我们就来参加这个 Activity

我使用 tabhost,它有 3 个选项卡,定义了以上 3 个 Activity 。单击选项卡时,我可以在各个 Activity 之间切换,但是当我使用表单上的按钮进行切换时,我看不到选项卡。我想在屏幕上显示标签,要么从标签或按钮切换。有没有像维护选项卡布局和仅更改框架中的 Activity 这样的概念......我听说过 tabhost 组但无法使用它......这有可能吗?坦克你

我的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);
}

}

最佳答案

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>

关于android - tabhost 安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13027651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com