Android入门之TabHost与TabWidget实例解析

 更新时间:2014年08月08日 11:33:16   投稿:shichen2014  
这篇文章主要介绍了Android入门之TabHost与TabWidget,对于Android初学者有一定的学习借鉴价值,需要的朋友可以参考下

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

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

先来贴出本例运行的截图:

main.xml的源码如下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
  android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/TabHost1">
  <TabWidget android:id="@android:id/tabs"
    android:layout_height="wrap_content" android:layout_width="fill_parent">
</TabWidget>
  <FrameLayout android:id="@android:id/tabcontent"
    android:paddingTop="65px" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab1" android:orientation="vertical" android:layout_width="fill_parent">
      <EditText android:layout_height="wrap_content" android:id="@+id/edtTab1" android:layout_width="fill_parent"></EditText>
      <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab1" android:text="Tab1"></Button>
    </LinearLayout>
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab2" android:layout_width="fill_parent" android:orientation="horizontal">
      <EditText android:layout_height="wrap_content" android:id="@+id/edtTab2" android:layout_width="wrap_content" android:layout_weight="300"></EditText>
      <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab2" android:text="Tab2"></Button></LinearLayout>
  </FrameLayout>
</TabHost>

java程序源码如下:

package com.testTab;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class testTab extends TabActivity {//基于TabActivity构建
 
 Button btnTab1,btnTab2;
 EditText edtTab1,edtTab2;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    TabHost tabs = getTabHost();
    //设置Tab1
    TabSpec tab1 = tabs.newTabSpec("tab1");
    tab1.setIndicator("tab1");   // 设置tab1的名称
    tab1.setContent(R.id.Tab1);  // 关联控件
    tabs.addTab(tab1);        // 添加tab1
    
    btnTab1=(Button)this.findViewById(R.id.btnTab1);
    edtTab1=(EditText)this.findViewById(R.id.edtTab1);
    btnTab1.setOnClickListener(new ClickEvent());
    
    //设置Tab2
    TabSpec tab2 = tabs.newTabSpec("tab2");
    tab2.setIndicator("tab2");   
    tab2.setContent(R.id.Tab2);  
    tabs.addTab(tab2);        
    
    btnTab2=(Button)this.findViewById(R.id.btnTab2);
    edtTab2=(EditText)this.findViewById(R.id.edtTab2);
    btnTab2.setOnClickListener(new ClickEvent());
    
    tabs.setCurrentTab(0);
  }
  
  class ClickEvent implements View.OnClickListener {
 @Override
 public void onClick(View v) {
  if(v==btnTab1)
  {
  edtTab1.setText("tab1");
  }
  else if(v==btnTab2)
  {
  edtTab2.setText("tab2");
  }
 }
  
  }
}

相关文章

  • Android 7.0 Nougat不得不知的11项新功能

    Android 7.0 Nougat不得不知的11项新功能

    不得不知的11项Android 7.0 Nougat新功能,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • android蓝牙控制PC端代码分享

    android蓝牙控制PC端代码分享

    这篇文章主要为大家分享了android蓝牙控制PC端的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • android AlertDialog的简单使用实例

    android AlertDialog的简单使用实例

    本篇文章主要介绍了android AlertDialog的简单使用实例,具有一定的参考价值,有兴趣的可以了解一下。
    2017-01-01
  • Android5.0中Material Design的新特性

    Android5.0中Material Design的新特性

    这篇文章主要介绍了Android5.0中Material Design的新特性的相关资料,需要的朋友可以参考下
    2016-08-08
  • Android中自定义水平进度条样式之黑色虚线

    Android中自定义水平进度条样式之黑色虚线

    这篇文章主要介绍了Android中自定义水平进度条样式之黑色虚线 的相关资料,需要的朋友可以参考下
    2016-03-03
  • Android实现自动填写获取验证码功能

    Android实现自动填写获取验证码功能

    这篇文章主要为大家详细介绍了Android实现自动填写获取验证码功能,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • AOSP源码下载示例代码

    AOSP源码下载示例代码

    这篇文章主要介绍了AOSP源码下载,由于清华源中给出很清晰的配置下载方法,这儿只做几条总结,对AOSP源码感兴趣的朋友一起看看吧
    2022-08-08
  • Android多国语言转换Excel及Excel转换为string详解

    Android多国语言转换Excel及Excel转换为string详解

    这篇文章主要给大家介绍了关于Android多国语言转换Excel以及Excel转换为string的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
    2019-01-01
  • 安卓(android)怎么实现下拉刷新

    安卓(android)怎么实现下拉刷新

    这里我们将采取的方案是使用组合View的方式,先自定义一个布局继承自LinearLayout,然后在这个布局中加入下拉头和ListView这两个子元素,并让这两个子元素纵向排列。对安卓(android)怎么实现下拉刷新的相关知识感兴趣的朋友一起学习吧
    2016-04-04
  • Android WebView 缓存详解

    Android WebView 缓存详解

    这篇文章主要介绍了 Android WebView 缓存详解的相关资料,需要的朋友可以参考下
    2017-06-06

最新评论