文/bywinkey 整理时间:2014年12月28日19:17:04
-
概述
在一个应用中,如果有多个标签页需要频繁切换时:例如 腾讯的微信和QQ,都是有用到多个功能页的切换,Android系统中提供了Tabhost组件来实现一个屏幕上多个页面的方便切换也显示。TabHost可以通过代码和布局文件来实现,布局文件的实现方法:TabHost分为<TabHost>和<TabWidget>在<TabWidget>里面放一个FrameLayout来存放每个页面的具体内容。(此片文章仅讲述使用布局文件生成)在使用tabHost的newTabSpec()方法将FrameLayout里面的东西add到Tab上即可 -
预备知识:
使用布局文件实现时:在布局文件中的 <TabWidget>的id必须定义为@android:id/tabs <FrameLayout>的id必须定义@android:id/tabcontent -
步骤:
-
新建Model
-
在布局文件中添加如下代码
<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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 单选题开始 --> <LinearLayout android:id="@+id/singleChoice" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="1.负责管理计算机硬件和软件资源,为应用程序的开发和运行提供高效平台的软件是?" android:textSize="18sp"/> <RadioGroup android:id="@+id/singleRG" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/optionA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A.操作系统" android:textSize="18sp" /> <RadioButton android:id="@+id/optionB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B.数据库管理系统" android:textSize="18sp" /> <RadioButton android:id="@+id/optionC" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C.编译系统" android:textSize="18sp" /> <RadioButton android:id="@+id/optionD" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="D.专用软件" android:textSize="18sp" /> </RadioGroup> </LinearLayout> <!-- 多选题开始 --> <LinearLayout android:id="@+id/multiChoice" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="1.激光打印机通常可以采用下面那些端口?" android:textSize="18sp"/> <CheckBox android:id="@+id/checkGoxA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A.并行接口" android:textSize="18sp"/> <CheckBox android:id="@+id/checkGoxB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B.USB接口" android:textSize="18sp"/> <CheckBox android:id="@+id/checkGoxC" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C.PS/2接口" android:textSize="18sp"/> <CheckBox android:id="@+id/checkGoxD" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="D.SCSI接口" android:textSize="18sp"/> </LinearLayout> <!-- 填空题开始 --> <LinearLayout android:id="@+id/fill" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="1.一幅分辨率为512x512的彩色图像,其R、G、B三个分量分别用8个二进制表示,则未压缩时该图像的数据容量是多少?" android:textSize="18sp"/> <EditText android:id="@+id/fillValue" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" android:hint="请输入答案." android:textSize="18sp"/> </LinearLayout> <!-- 判断题开始 --> <LinearLayout android:id="@+id/judge" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="1.程序就是算法,算法就是程序" android:textSize="18sp" /> <RadioGroup android:id="@+id/judgeoptionRG" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/judgeoptionA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="对" android:textSize="18sp" /> <RadioButton android:id="@+id/judgeoptionB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="错" android:textSize="18sp" /> </RadioGroup> <TextView android:id="@+id/score" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="您的得分45分"/> <Button android: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;//判断题
@Override
protected 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按钮并设置其对应的layout
tabHost.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() {
@Override
public 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() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(judgeT.getId() == checkedId){
judge = 1;
}else if(judgeF.getId() == checkedId){
judge = 2;
}
}
});
}
/**
* 提交事件
*/
class BtnListener implements View.OnClickListener{
@Override
public 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();
}
}
@Override
public 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;
}
@Override
public 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 SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
关于addTab的说明:
addTable(tab.Host.newTabSpce(“标签分类名称”).setIndicator((“显示的名称”),getDrawable(要显示的图片资源文件)).setContent(要分配的页面上的Layoutid));