文/bywinkey 整理时间:2014年12月28日19:17:04

  1. 概述
    在一个应用中,如果有多个标签页需要频繁切换时:例如 腾讯的微信和QQ,都是有用到多个功能页的切换,Android系统中提供了Tabhost组件来实现一个屏幕上多个页面的方便切换也显示。TabHost可以通过代码和布局文件来实现,布局文件的实现方法:TabHost分为<TabHost>和<TabWidget>在<TabWidget>里面放一个FrameLayout来存放每个页面的具体内容。(此片文章仅讲述使用布局文件生成)在使用tabHost的newTabSpec()方法将FrameLayout里面的东西add到Tab上即可

  2. 预备知识:
    使用布局文件实现时:在布局文件中的 <TabWidget>的id必须定义为@android:id/tabs <FrameLayout>的id必须定义@android:id/tabcontent

  3. 步骤:

    1. 新建Model

    2. 在布局文件中添加如下代码

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Mytabhost"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TabHostandroid:id="@+id/tabhost"android:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"><TabWidgetandroid:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"/><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 单选题开始 --><LinearLayoutandroid:id="@+id/singleChoice"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="1.负责管理计算机硬件和软件资源,为应用程序的开发和运行提供高效平台的软件是?"android:textSize="18sp"/><RadioGroupandroid:id="@+id/singleRG"android:layout_width="wrap_content"android:layout_height="wrap_content"><RadioButtonandroid:id="@+id/optionA"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="A.操作系统"android:textSize="18sp"/><RadioButtonandroid:id="@+id/optionB"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="B.数据库管理系统"android:textSize="18sp"/><RadioButtonandroid:id="@+id/optionC"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="C.编译系统"android:textSize="18sp"/><RadioButtonandroid:id="@+id/optionD"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="D.专用软件"android:textSize="18sp"/></RadioGroup></LinearLayout><!-- 多选题开始 --><LinearLayoutandroid:id="@+id/multiChoice"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="1.激光打印机通常可以采用下面那些端口?"android:textSize="18sp"/><CheckBoxandroid:id="@+id/checkGoxA"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="A.并行接口"android:textSize="18sp"/><CheckBoxandroid:id="@+id/checkGoxB"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="B.USB接口"android:textSize="18sp"/><CheckBoxandroid:id="@+id/checkGoxC"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="C.PS/2接口"android:textSize="18sp"/><CheckBoxandroid:id="@+id/checkGoxD"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="D.SCSI接口"android:textSize="18sp"/></LinearLayout><!-- 填空题开始 --><LinearLayoutandroid:id="@+id/fill"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="1.一幅分辨率为512x512的彩色图像,其R、G、B三个分量分别用8个二进制表示,则未压缩时该图像的数据容量是多少?"android:textSize="18sp"/><EditTextandroid:id="@+id/fillValue"android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="number"android:hint="请输入答案."android:textSize="18sp"/></LinearLayout><!-- 判断题开始 --><LinearLayoutandroid:id="@+id/judge"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="1.程序就是算法,算法就是程序"android:textSize="18sp"/><RadioGroupandroid:id="@+id/judgeoptionRG"android:layout_width="wrap_content"android:layout_height="wrap_content"><RadioButtonandroid:id="@+id/judgeoptionA"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="对"android:textSize="18sp"/><RadioButtonandroid:id="@+id/judgeoptionB"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="错"android:textSize="18sp"/></RadioGroup><TextViewandroid:id="@+id/score"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="您的得分45分"/><Buttonandroid:id="@+id/submitBtn"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="提交"/></LinearLayout></FrameLayout><!-- tabcontent内容 --></LinearLayout><!-- 存放TabHost --></TabHost><!-- tabHost --></LinearLayout>
      </RelativeLayout>
      

      c).在Activity中添加如下代码:

package com.winkey_yao.com.mytabhost;import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;public class Mytabhost extends ActionBarActivity {private Button submitBtn;    //提交按钮private TextView score;       //分数显示private RadioGroup singleRG; //单选题组private RadioButton optionA,optionB,optionC,optionD;//单选题的按钮private int single = 0;//单选题选择项目private int judge = 0;//判断题private CheckBox ckbox1,ckbox2,ckbox3,ckbox4;//多选题private EditText inputText;//天空题答案private RadioGroup judgeRG;//判断题private RadioButton judgeT,judgeF;//判断题@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_mytabhost);TabHost tabHost = (TabHost) findViewById(R.id.tabhost);//找到tabhost节点tabHost.setup();//通过setup启动和加载//给tabhost添加 tab按钮并设置其对应的layouttabHost.addTab(tabHost.newTabSpec("tab01").setIndicator(("单选题"),getResources().getDrawable(R.drawable.singleimg)).setContent(R.id.singleChoice));tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator(("多选题"),getResources().getDrawable(R.drawable.singleimg)).setContent(R.id.multiChoice));tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator(("填空题"),getResources().getDrawable(R.drawable.singleimg)).setContent(R.id.fill));tabHost.addTab(tabHost.newTabSpec("tab04").setIndicator(("判断题"),getResources().getDrawable(R.drawable.singleimg)).setContent(R.id.judge));//单选题singleRG = (RadioGroup) findViewById(R.id.singleRG);//单选题按钮optionA = (RadioButton) findViewById(R.id.optionA);optionB = (RadioButton) findViewById(R.id.optionB);optionC = (RadioButton) findViewById(R.id.optionC);optionD = (RadioButton) findViewById(R.id.optionD);//多项按钮ckbox1 = (CheckBox) findViewById(R.id.checkGoxA);ckbox2 = (CheckBox) findViewById(R.id.checkGoxB);ckbox3 = (CheckBox) findViewById(R.id.checkGoxC);ckbox4 = (CheckBox) findViewById(R.id.checkGoxD);//填空题答案inputText = (EditText) findViewById(R.id.fillValue);//判断题judgeRG = (RadioGroup) findViewById(R.id.judgeoptionRG);judgeT = (RadioButton) findViewById(R.id.judgeoptionA);judgeF = (RadioButton) findViewById(R.id.judgeoptionB);//分数显示组件score = (TextView) findViewById(R.id.score);//提交按钮submitBtn = (Button) findViewById(R.id.submitBtn);submitBtn.setOnClickListener(new BtnListener());singleRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(optionA.getId() == checkedId){single = 1;}else if(optionB.getId() == checkedId){single = 2;}else if(optionC.getId() == checkedId){single = 3;}else if(optionD.getId() == checkedId){single = 4;}}});judgeRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(judgeT.getId() == checkedId){judge = 1;}else if(judgeF.getId() == checkedId){judge = 2;}}});}/*** 提交事件*/class BtnListener implements View.OnClickListener{@Overridepublic void onClick(View v) {int allscore = 0;//总分if(single == 0){//单项选择提没有Toast.makeText(Mytabhost.this,"单项选择提,没有做!",Toast.LENGTH_LONG).show();return;}if(!ckbox1.isChecked() && !ckbox2.isChecked() && !ckbox3.isChecked() && !ckbox4.isChecked()){print("多项选择题没做!");return;}if(single == 1){allscore += 20;}if(ckbox1.isChecked() && ckbox2.isChecked() && !ckbox3.isChecked() && ckbox4.isChecked()){//全选中 给 40分allscore += 40;}else{//如果没有全对 //选对一个 +  13 分if(ckbox1.isChecked()){allscore += 13;}if(ckbox2.isChecked()){allscore += 13;}if(ckbox4.isChecked()){allscore += 13;}if(ckbox3.isChecked()){allscore -= 13;}}if(inputText.getText() == null || "".equals(inputText.getText().toString())){print("填空题没做!");return;}int inputValue = Integer.parseInt(inputText.getText().toString());if(inputValue == 786432){allscore += 20;}if(judge == 0){print("判断题没做!");return;}if(judge == 2){allscore += 20;}score.setText("您的得分:" + allscore);}private void print(String args){Toast.makeText(Mytabhost.this,args,Toast.LENGTH_LONG).show();}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menu_mytabhost, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();//noinspection SimplifiableIfStatementif (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}
}

关于addTab的说明:

addTable(tab.Host.newTabSpce(“标签分类名称”).setIndicator((“显示的名称”),getDrawable(要显示的图片资源文件)).setContent(要分配的页面上的Layoutid));

android学习之五·使用系统组件TabHost(使用布局文件)相关推荐

  1. Android 学习之路 四大组件之Activity1(内置人品计算器)

    前言: 终于到了四大组件,打掉前面的"小怪",来到安卓的核心boss,开心. 1 创建一个新的Activity    1>如果你想让你的Activity有多个启动图标 需要这 ...

  2. Android学习之调用系统相机实现拍照功能

    一.今天,来介绍如何调用系统自带的相机进行拍照,主要有以下2种实现的方式: 1.Camera应用程序包含了一个意图过滤器,即intent filter,它使得开发人员能够提供与Camera应用程序同等 ...

  3. 3.Android学习之常用UI组件(一)

    目录 3.常用UI组件(一) 1.文本类组件 1-1.文本框(TextView) 1-2.编辑框(EditText) 2.按钮类组件 2-1.普通按钮(Button) 2-2.图片按钮(ImageBu ...

  4. Android学习笔记(一)——控件布局常用属性

    LinearLayout线性布局 id:为控件指定相应的ID. width:控件的宽度. height:控件的高度. background:背景颜色. Orientation:控件的排列方向(默认是水 ...

  5. Android学习问题:关于AlertDialog中自定义布局带有的EditText无法弹出键盘

    最近在用到AlertDialog的时候,自定义了其布局文件,其中带有EditText,但是发现在手机上使用的时候,点击EditText却没有弹出键盘,在网上搜索了下,看到有好几种解决方法,试过其中一些 ...

  6. Android开发笔记(七十四)布局文件优化

    include/merge 布局优化中常常用到include/merge标签,include的含义类似C代码中的include,意思是直接把指定布局片段包含进当前的布局文件.include适用于多个布 ...

  7. osg for android 学习之五:场景漫游

    需要实现这个,先把参考的文章列在这里 http://blog.csdn.net/tinya0913/article/details/6124167 效果很不错哦,希望在android上可以实现. /* ...

  8. android 搜索文件代码怎么写,android学习笔记(5)-一个搜索文件的APP(2)-搜索功能的实现...

    接上一篇,今天把搜索的代码放上去了.效果图如下. MainActivity.java package com.stk.afinder; import android.os.Bundle; import ...

  9. android学习笔记(5)-一个搜索文件的APP(2)-搜索功能的实现

    接上一篇,今天把搜索的代码放上去了.效果图如下. MainActivity.java package com.stk.afinder;import android.os.Bundle; import ...

最新文章

  1. SQL SERVER 2008 登陆失败(SQL和windows都没有对应的权限)
  2. lightoj 1224
  3. Android 透明动画实现 详细概述
  4. mybatis-java-依赖注入
  5. 百度论文引用网络节点分类比赛
  6. linux dvd 刻录_如何将任何视频文件刻录到可播放的DVD
  7. 设计模式——责任链模式(职责链模式)
  8. Python的pyhanlp库使用(自然语言识别、姓名)
  9. [AHK]爬虫基础 解析IP138网站返回的结果
  10. 新浪微博回调地址redirect_url(授权回调页)的设置格式
  11. matlab trapz二重积分函数_「matlab 积分」使用Matlab求解定积分/不定积分 - seo实验室...
  12. 字节跳动原来这么容易就能进去…
  13. Python破解百度翻译反爬机制---自制翻译器
  14. 亲身试验力荐:番茄工作法
  15. css动态飞飞荷包蛋
  16. 热电阻 热电偶 测量电路_热电偶温度传感器与热电阻温度传感器之间应该如何选择?...
  17. 想看看18001年以后的金山词霸吗?金山词霸之未来版
  18. 【数据压缩】H.264文件解析和码流分析
  19. 计算机编程职称论文,自动化工程师职称论文
  20. P2141 珠心算测验(C语言)

热门文章

  1. 凯撒密码(Caesar)的原理和算法实现(C语言)
  2. iwcコピー時計の全く新しい「La Musicale」音楽の腕時計
  3. clientWidth与offsetWidth与scrollWidth
  4. 阿里的德鲁伊怎么玩?
  5. 微信小程序中转换时间格式IOS不兼容的问题
  6. linux dhcp option43,Linux DHCP通过OPTION43为H3C的AP下发AC地址
  7. 刷脸支付代理加盟需要注意哪些事项
  8. 刷脸支付服务商加入支付行业分一杯羹
  9. 《OpenCV》配置多个C++版本的opencv(Ubuntu18.04)
  10. Leetcode 柠檬水找零