我目前正在试用 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/