[英]Android 1.5 Programming with TabHost and Buttons
I am currently trying out the Android 1.5 SDK and I have seen a couple of examples on TabHost. 我目前正在尝试Android 1.5 SDK,我在TabHost上看到了几个例子。 What I am trying to do is to use a different button on each Tab to do its tasks. 我想要做的是在每个Tab上使用不同的按钮来完成它的任务。
What I tried was using onClickListiner() and onClick(). 我尝试的是使用onClickListiner()和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. 我认为这是所有开发人员使用的,但每次按下按钮时,我都会在LogCat上得到一个null异常。 Also I have each XML Layout so I call the Tab as : tab.add(...setContent(R.id.firstTabLayout)) 我也有每个XML布局,所以我将Tab称为: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? 在TabHost下使按钮/ TextView正常工作的最佳方法是什么?
I'm not entirely sure where your problem is, but this is how I've set up my Tabbed activities before 我不完全确定你的问题在哪里,但这就是我之前设置Tabbed活动的方式
layout.xml 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 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)
然后,您可以在选项卡中的任何视图上使用findViewById(),或者如果它们之间有共享名称,您可以执行findViewById(R.id.tab1).findViewById(R.id.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.