使用 TabHost 和按钮进行 Android 1.5 编程

发布于 2024-07-25 10:04:41 字数 377 浏览 2 评论 0原文

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

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

firstTabLayout = layout for Button and TextView.

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

I am currently trying out the Android 1.5 SDK and I have seen a couple of examples on TabHost.
What I am trying to do is to use a different button on each Tab to do its tasks.

What I tried
was using onClickListiner() and onClick(). I think this is what all the developers use, but I keep getting a null exception on the LogCat every time the button is pressed. Also I have each XML Layout so I call the Tab as : tab.add(...setContent(R.id.firstTabLayout))

firstTabLayout = layout for Button and TextView.

What would be the best way to make a button/TextView work properly under the TabHost?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2024-08-01 10:04:41

之前设置选项卡式活动的方式

我不完全确定你的问题出在哪里,但这就是我在layout.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));
    }
}

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

I'm not entirely sure where your problem is, but this is how I've set up my Tabbed activities before

layout.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));
    }
}

Then, you can just use findViewById() on any views within the tabs, or if there are shared names between them, you can do findViewById(R.id.tab1).findViewById(R.id.text)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文