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

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE