Android 1.5的编程与TabHost和按钮
-
21-08-2019 - |
题
目前我正在试图走出了Android 1.5的SDK我见过一对夫妇对TabHost例子。 我所试图做的是使用上的每个选项卡不同的按钮来完成其任务。
我尝试什么 使用onClickListiner()和的onClick()。我想这是所有的开发者使用什么,但我一直在每次按下按钮时得到的logcat的一个空例外。另外我有每个XML布局,所以我呼叫的选项卡为:tab.add(... setContent(R.id.firstTabLayout))
firstTabLayout = layout for Button and TextView.
什么是使一个按钮/ TextView的正常工作的TabHost?
下的最佳途径解决方案
我不完全知道从哪里你的问题,但是这是我以前怎么
设置我的分页活动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)
不隶属于 StackOverflow