1 介绍
安卓中TAB页的功能与WEB开发类似,就是利用空间复用展示更多的信息,使用Tab标签页控件,可以在同一个空间里放置更多内容。
TabActivity继承Activity,主要功能是实现多个activity或者view之间的切换和显示,要使用该类必须新建一个类来继承TabActivity,并且该类的xml配置文件中必须包含TabHost、TabWidget、FrameLayout三个视图(View),其中后面两个标签是前面一个标签的子标签,表示tab页的选项卡,相当于菜单页,表示显示内容的区域。
2 函数
public TabHost getTabHost () //获得当前TabActivity的TabHost
public TabWidget getTabWidget () //获得当前TabActivity 的TabWidget
public void setDefaultTab (String tag) //设置默认的Tab
public void setDefaultTab (int index) //通过tab名——tag或者index(从0开始)
protected void onRestoreInstanceState (Bundle state)
protected void onSaveInstanceState (Bundle outState)
//这两个函数的介绍可以参考 Activity的生命周期
3 代码参考
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="65dp">
<!-- LinearLayout 是一个标签里的内容,程序根据ID来把他们放在不同的标签下面 -->
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ListView
android:id="@+id/list_detailviews1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp" />
</LinearLayout>
<!-- LinearLayout 是另一个标签里的内容-->
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ListView
android:id="@+id/list_detailviews2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp" />
</LinearLayout>
</FrameLayout>
</TabHost>
代码程序参考:
// tabHost是一个标签容器
TabHost tabHost = this.getTabHost();
// 每一个TabSpec对象就是个标签
// TabSpec.setIndicator()方法是设置标签显示样式
// TabSpec.setContent()方法显示标签下方的内容显示
//定义第一个标签
tabHost.addTab(tabHost.newTabSpec("1st")
.setIndicator("1st", getResources().getDrawable(android.R.drawable.star_on))
.setContent(R.id.linearLayout1));
//定义第二个标签
tabHost.addTab(tabHost.newTabSpec("2nd")
.setIndicator("2nd", getResources().getDrawable(android.R.drawable.star_on))
.setContent(R.id.linearLayout2));
private void initView1() {
//得到ListView,在这里写业务代码
mListView1 = (ListView) findViewById(R.id.list_detailviews1);
}
4 补充
Android中EditText判断是否为空:
EditView edit = (EditView)findViewById(R.id.text);
TextUtils.isEmpty(edit.getText());
Android 中如何获取editText文本信息:
EditText editText1 =(EditText) findViewById (R.id.editText1);
c=Integer.parseInt(editText1.getText().toString());
参考资料:
http://www.jizhuomi.com/android/course/300.html 用户界面View之Tab标签页
https://blog.csdn.net/sunshine0216/article/details/72864439 TabActivity简介及实例使用