java - 使用 TabHost 和按钮的 Android 1.5 编程

标签 java android mobile sdk android-tabhost

我目前正在试用 Android 1.5 SDK,并且在 TabHost 上看到了几个示例。 我想要做的是在每个选项卡上使用不同的按钮来完成它的任务。

我试过的 正在使用 onClickListiner() 和 onClick()。我认为这是所有开发人员使用的,但每次按下按钮时,我都会在 LogCat 上收到空异常。我还有每个 XML 布局,所以我将 Tab 称为:tab.add(...setContent(R.id.firstTabLayout))

firstTabLayout = layout for Button and TextView.

使按钮/TextView 在 TabHost 下正常工作的最佳方法是什么?

最佳答案

我不完全确定你的问题出在哪里,但这是我之前设置选项卡式 Activity 的方式

布局.xml

<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="fill_parent">

    <LinearLayout android:id="@+id/tab1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView android:id="@android:id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <LinearLayout android:id="@+id/tab2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView android:id="@android:id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <LinearLayout android:id="@+id/tab3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView android:id="@android:id/text"
            android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

    </LinearLayout>
</FrameLayout>

Tab.java

public class InitialActivity extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        TabHost th = getTabHost();
        th.addTab(th.newTabSpec("tab_1").setIndicator("Tab1").setContent(R.id.tab1));
        th.addTab(th.newTabSpec("tab_2").setIndicator("Tab2").setContent(R.id.tab2));
        th.addTab(th.newTabSpec("tab_3").setIndicator("Tab3").setContent(R.id.tab3));
    }
}

然后,您可以在选项卡中的任何 View 上使用 findViewById(),或者如果它们之间有共享名称,您可以执行 findViewById(R.id.tab1).findViewById(R.id.text )

关于java - 使用 TabHost 和按钮的 Android 1.5 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1066008/

相关文章:

Android 从 WorkManager 队列中删除工作人员或将状态从 FAILED 更改为 CANCELED

android - 移动应用程序和 WooCommerce WordPress 网站数据同步

html - 可滚动侧边栏不显示移动设备上的所有内容

java - 尝试使用 HttpExchange 实现示例

java - java webstart应用程序如何防止盗版

java - 此处不允许使用 void 类型 (java) (s != null)

java - 添加从列表返回的元素

android - 在 Python 和 Kivy 中使用 buildozer 时出现错误 "Unable to find Compiler"

java - 如何解决com.android.internal.R.drawable.hover_tooltip_bg?

javascript - jQuery Mobile 下拉菜单导致页面重新加载