繁体   English   中英

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

[英]Android 1.5 Programming with TabHost and Buttons

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

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

firstTabLayout = layout for Button and TextView.

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

我不完全确定你的问题在哪里,但这就是我之前设置Tabbed活动的方式

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM