zoukankan      html  css  js  c++  java
  • TabSpec和TabHost实例

    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()设置的标题

  • 相关阅读:
    JVM垃圾回收机制
    浅谈类的几个基础构造函数
    三次登陆用户名 和密码
    干迷宫
    记录人生中的第一个bug
    js三元运算符? :
    Linux云服务器 磁盘分区失败
    python基础中遇到的问题(TypeError: unhashable type: 'list')
    python亲密数设计
    源码编译安装Protobuf
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5225019.html
Copyright © 2011-2022 走看看