【问题标题】:Android 1.5 Programming with TabHost and Buttons使用 TabHost 和按钮进行 Android 1.5 编程
【发布时间】:2009-06-30 20:54:52
【问题描述】:
我目前正在试用 Android 1.5 SDK,并且在 TabHost 上看到了几个示例。
我想要做的是在每个选项卡上使用不同的按钮来完成它的任务。
我尝试了什么
正在使用 onClickListiner() 和 onClick()。我认为这是所有开发人员都使用的,但是每次按下按钮时,我都会在 LogCat 上收到一个空异常。我也有每个 XML 布局,所以我将选项卡称为:tab.add(...setContent(R.id.firstTabLayout))
firstTabLayout = layout for Button and TextView.
使按钮/TextView 在 TabHost 下正常工作的最佳方法是什么?
【问题讨论】:
标签:
java
android
mobile
sdk
android-tabhost
【解决方案1】:
我不完全确定您的问题出在哪里,但这就是我之前设置选项卡式活动的方式
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)