TabSpec与TabHost

TabHost相当于浏览器中浏览器分布的集合,而Tabspec则相当于浏览器中的每一个分页面。d在Android中,每一个TabSpec分布可以是一个组件,也可以是一个布局,然后将每一个分页装入TabHost中,TabHost即可将其中的每一个分页一并显示出来。

步骤:

(1)继承TabActivity:在此之前继承的都是android.app.Activity类,但是这里需要继承android.app.TabActivity。

(2)创建TabHost分布菜单对象,利用以下代码。

LayoutInflater.from(this).inflate(R.layout.main,tableHost.getTabContentView());

(3)实例化实分页

java代码:

package com.test;

import android.app.Activity;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
import android.widget.TabHost.TabSpec; public class MainActivity extends TabActivity implements OnTabChangeListener {
/** Called when the activity is first created. */
private TabSpec ts1 ,ts2, ts3 ;//声明3个分页
private TabHost tableHost;//分页菜单(tab的容器)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tableHost = this.getTabHost();//实例 (分页)菜单
//利用LayoutInflater将布局与分页菜单一起显示
LayoutInflater.from(this).inflate(R.layout.main, tableHost.getTabContentView());
ts1 = tableHost.newTabSpec("tabOne");//实例化一个分页
ts1.setIndicator("Tab1");//设置此分页显示的标题
ts1.setContent(R.id.btn);//设置此分页的资源id
ts2=tableHost.newTabSpec("tabTwo");//实例化第二个分页
//设置此分页显示的标题和图标
ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.icon));
ts2.setContent(R.id.et);
ts3= tableHost.newTabSpec("tabThree");//实例化第三个分页
ts3.setIndicator("Tab3");
ts3.setContent(R.id.mylayout);//设置此分页的布局id
tableHost.addTab(ts1);//菜单中添加ts1分页
tableHost.addTab(ts2);
tableHost.addTab(ts3);
tableHost.setOnTabChangedListener(this);
}
public void onTabChanged(String tabId){
if(tabId.equals("tabOne")){
Toast.makeText(this, "分页1", Toast.LENGTH_LONG).show();
}
if(tabId.equals("tabTwo")){
Toast.makeText(this, "分页2", Toast.LENGTH_LONG).show();
}
if(tabId.equals("tabThree")){
Toast.makeText(this, "分页3", Toast.LENGTH_LONG).show();
}
}
}

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="This is Tab1"
/>
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is Tab2"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mylayout"
android:background="@drawable/bg"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is Tab3"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is Tab3"
/>
</LinearLayout>
</LinearLayout>

运行结果:

总结:监听分页改变事件,具体如下:

1、使用OnTabChangeListener接口,重写OnTabChanged(String tabId)函数

2、TabHost绑定监听器

3、判断OnTabChanged(String tabId)中的tabId参数进行处理事件;这里的tabId对应的是实例中每个分页传入的分页ID,而不是TabSpec.setIndicatior()设置的标题

TabSpec和TabHost实例的更多相关文章

  1. Android 轮换页面+TabHost 实例

    最终效果展示: 首先我们需要一个ViewPager控件,不过可以发现在左侧的控件列表中并没有这个控件 这时我们要去升级包中查看 然后在厘米找到 ViewPager.class 这时我们双击这个发现不能 ...

  2. android学习日记03--常用控件tabSpec/tabHost

    常用控件7.TabSpec和TabHost 比较常用的控件,感觉手机QQ的整体布局就是这个,只不过tab放在底部而已.TabSpec相当于浏览器的分页,而TabHost就相当于分页的集合TabSpec ...

  3. 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)

    3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.htm ...

  4. android学习笔记十——TabHost

    TabHost——标签页 ==> TabHost,可以在窗口放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组件摆放区域. 通过此种方式可以实现在一个容器放置更多组件(EG:通话记 ...

  5. 转-TabHost组件(一)(实现底部菜单导航)

    http://www.cnblogs.com/lichenwei/p/3974009.html 什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用wind ...

  6. 转-TabHost组件(二)(实现底部菜单导航)

    http://www.cnblogs.com/lichenwei/p/3975095.html 上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定 ...

  7. Android:TabHost实现Tab切换

    TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容. 实现方式有两种: 1.继承TabA ...

  8. 选项卡(TabHost)的功能与用法

    TabHost是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个便签页,每个标签页相当于获得了一个与外部容器相同大小的组件摆法区域.通过这种方式,就可以在一个容器里放置更多组件,例如手机 ...

  9. Android——TabHost(标签容器)相关知识总结贴

    android 2.3 r1 中文 api (58) —— TabHost http://www.apkbus.com/android-18911-1-1.html   android中文api (5 ...

随机推荐

  1. 简单使用SimpleCursorAdapter

    http://my.oschina.net/javaeye/blog/14846 果使用Sqlite,建议和ContentProvider结合使用.这样数据库的生命周期就不用自己管了.然后,如果要在比 ...

  2. IOS学习之蓝牙4.0 BLE

    IOS学习也一段时间了,该上点干货了.前段时间研究了一下IOS蓝牙通讯相关的东西,把研究的一个成果给大家分享一下. 一 项目背景 简单介绍一下做的东西,设备是一个金融刷卡器,通过蓝牙与iphone手机 ...

  3. Lable 控件 -- 用代码改变要显示字体的颜色

    lable控件怎么改变显示字体的颜色 代码如下: string color = "#B72C34"; this.lbl.ForeColor = System.Drawing.Col ...

  4. mvc模式jsp+servel+jdbc oracle基本增删改查demo

    mvc模式jsp+servel+jdbc oracle基本增删改查demo 下载地址

  5. C++ 字符串指针与字符串数组

    在做面试100题中第21题时,发现char *astr="abcdefghijk\0";和char astr[]={"abcdefghijk"};有点区别,以前 ...

  6. Tomcat 启动 Debug模式

    如果debug启动遇到如下错误: ERROR: transport error 202: gethostbyname: unknown host ERROR: JDWP Transport dt_so ...

  7. iphone手机上的click和touch

    在iphone手机上绑定click事件时,当你触发点击事件时,你绑定的click事件的DOM节点,会自动被一块浮层选中.所以如果使用事件委托来做事件绑定会造成很差的用户体验. 使用touchstart ...

  8. 使用PHP实现用户登录和注册的功能

    登陆界面 login.php <form action="logincheck.php" method="post"> 用户名:<input ...

  9. 自己动手写 ASP.NET MVC 分页 part1

    学习编程也有一年半载了,从来没有自己动手写过东西,都是利用搜索软件找代码,最近偶发感慨,难道真的继续做码农??? 突发奇想是不是该自己动手写点东西,可是算法.逻辑思维都太弱了,只能copy网上的代码, ...

  10. 哈希长度扩展攻击的简介以及HashPump安装使用方法

    哈希长度扩展攻击(hash length extension attacks)是指针对某些允许包含额外信息的加密散列函数的攻击手段.该攻击适用于在消息与密钥的长度已知的情形下,所有采取了 H(密钥 ∥ ...