Android入门第十一篇之TabHost,TabWidget

2024-04-22 18:08

本文主要是介绍Android入门第十一篇之TabHost,TabWidget,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 本文来自:http://blog.csdn.net/hellogv/article/details/5958565

        这回要介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要的时候再使用。Android的Tab控件使用起来有点奇怪,必须包含和按照以下的顺序:

TabHost控件->TabWidget(必须命名为tabs)->FrameLayout(必须命名为tabcontent)。

 

接下来贴出本例运行的截图:

 

main.xml的源码:

 

[xhtml] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/TabHost1">  
  4.     <TabWidget android:id="@android:id/tabs"  
  5.         android:layout_height="wrap_content" android:layout_width="fill_parent">  
  6. </TabWidget>  
  7.     <FrameLayout android:id="@android:id/tabcontent"  
  8.         android:paddingTop="65px" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  9.         <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab1" android:orientation="vertical" android:layout_width="fill_parent">  
  10.            <EditText android:layout_height="wrap_content" android:id="@+id/edtTab1" android:layout_width="fill_parent"></EditText>  
  11.            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab1" android:text="Tab1"></Button>  
  12.         </LinearLayout>  
  13.         <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab2" android:layout_width="fill_parent" android:orientation="horizontal">  
  14.            <EditText android:layout_height="wrap_content" android:id="@+id/edtTab2" android:layout_width="wrap_content" android:layout_weight="300"></EditText>  
  15.            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab2" android:text="Tab2"></Button></LinearLayout>  
  16.     </FrameLayout>  
  17. </TabHost>  

 

程序源码:

[java] view plain copy print ?
  1. package com.testTab;  
  2. import android.app.TabActivity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6. import android.widget.EditText;  
  7. import android.widget.TabHost;  
  8. import android.widget.TabHost.TabSpec;  
  9. public class testTab extends TabActivity {//基于TabActivity构建   
  10.       
  11.     Button btnTab1,btnTab2;  
  12.     EditText edtTab1,edtTab2;  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         TabHost tabs = getTabHost();  
  20.         //设置Tab1   
  21.         TabSpec tab1 = tabs.newTabSpec("tab1");  
  22.         tab1.setIndicator("tab1");      // 设置tab1的名称   
  23.         tab1.setContent(R.id.Tab1);    // 关联控件   
  24.         tabs.addTab(tab1);                // 添加tab1   
  25.           
  26.         btnTab1=(Button)this.findViewById(R.id.btnTab1);  
  27.         edtTab1=(EditText)this.findViewById(R.id.edtTab1);  
  28.         btnTab1.setOnClickListener(new ClickEvent());  
  29.           
  30.         //设置Tab2   
  31.         TabSpec tab2 = tabs.newTabSpec("tab2");  
  32.         tab2.setIndicator("tab2");        
  33.         tab2.setContent(R.id.Tab2);      
  34.         tabs.addTab(tab2);                  
  35.           
  36.         btnTab2=(Button)this.findViewById(R.id.btnTab2);  
  37.         edtTab2=(EditText)this.findViewById(R.id.edtTab2);  
  38.         btnTab2.setOnClickListener(new ClickEvent());  
  39.           
  40.         tabs.setCurrentTab(0);  
  41.     }  
  42.       
  43.     class ClickEvent implements View.OnClickListener {  
  44.         @Override  
  45.         public void onClick(View v) {  
  46.             if(v==btnTab1)  
  47.             {  
  48.                 edtTab1.setText("tab1");  
  49.             }  
  50.             else if(v==btnTab2)  
  51.             {  
  52.                 edtTab2.setText("tab2");  
  53.             }  
  54.         }  
  55.       
  56.     }  
  57. }  

这篇关于Android入门第十一篇之TabHost,TabWidget的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/926486

相关文章

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Java List 使用举例(从入门到精通)

《JavaList使用举例(从入门到精通)》本文系统讲解JavaList,涵盖基础概念、核心特性、常用实现(如ArrayList、LinkedList)及性能对比,介绍创建、操作、遍历方法,结合实... 目录一、List 基础概念1.1 什么是 List?1.2 List 的核心特性1.3 List 家族成

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

史上最全MybatisPlus从入门到精通

《史上最全MybatisPlus从入门到精通》MyBatis-Plus是MyBatis增强工具,简化开发并提升效率,支持自动映射表名/字段与实体类,提供条件构造器、多种查询方式(等值/范围/模糊/分页... 目录1.简介2.基础篇2.1.通用mapper接口操作2.2.通用service接口操作3.进阶篇3

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Python实现Word转PDF全攻略(从入门到实战)

《Python实现Word转PDF全攻略(从入门到实战)》在数字化办公场景中,Word文档的跨平台兼容性始终是个难题,而PDF格式凭借所见即所得的特性,已成为文档分发和归档的标准格式,下面小编就来和大... 目录一、为什么需要python处理Word转PDF?二、主流转换方案对比三、五套实战方案详解方案1:

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.