簡體   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