码迷,mamicode.com
首页 > 移动开发 > 详细

安卓初級教程(5):TabHost的思考

时间:2015-07-18 22:38:49      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.myhost;
 2 
 3 import android.os.Bundle;
 4 import android.view.LayoutInflater;
 5 import android.widget.TabHost;
 6 import android.app.TabActivity;
 7 
 8 public class TabhostActivity extends TabActivity {    // Extend TabActivity class
 9 
10     /** Called when the activity is first created. */
11 
12     @Override
13 
14     public void onCreate(Bundle savedInstanceState) {
15 
16         super.onCreate(savedInstanceState);
17 
18        TabHost tabHost = this.getTabHost();  // The activity TabHost
19         LayoutInflater.from(this).inflate(R.layout.main,tabHost.getTabContentView(), true);
20 
21         tabHost.addTab(tabHost.newTabSpec("tab1")
22                 .setIndicator("tab1", getResources()
23                         .getDrawable(R.drawable.chartareaup_green))
24                         .setContent(R.id.linearLayout1));
25 
26         tabHost.addTab(tabHost.newTabSpec("tab2")
27                 .setIndicator("tab2", getResources()
28                         .getDrawable(R.drawable.home_yellow))
29                         .setContent(R.id.linearLayout2));     
30 
31         tabHost.addTab(tabHost.newTabSpec("tab3")
32                 .setIndicator("tab3", getResources()
33                         .getDrawable(R.drawable.harddrivedownload_red))
34                         .setContent(R.id.linearLayout3));    
35 
36         tabHost.addTab(tabHost.newTabSpec("tab4")
37                 .setIndicator("tab4", getResources()
38                         .getDrawable(R.drawable.shoppingcart_blue))
39                         .setContent(R.id.linearLayout4));
40     }
41 }
 1 package net.weibo.ui;
 2 
 3 import android.app.TabActivity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.util.Log;
 7 import android.widget.RadioGroup;
 8 import android.widget.RadioGroup.OnCheckedChangeListener;
 9 import android.widget.TabHost;
10 import android.widget.TabHost.TabSpec;
11 
12 public class MainActivity extends TabActivity {
13     /** Called when the activity is first created. */
14     public TabHost mth;
15     public static final String TAB_HOME="HOME";
16     public static final String TAB_MSG="INFORMATION";
17     public static final String TAB_ABT="ABOUT";
18     public static final String TAB_SEH="SEARCH";
19     public static final String TAB_MORE="MORE";
20 
21     public RadioGroup radioGroup;
22 
23 
24     @Override
25     public void onCreate(Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27         setContentView(R.layout.main);
28 
29         mth=this.getTabHost(); //mth 等於上一段第18行的寫法。
30 
31         TabSpec ts1=mth.newTabSpec(TAB_HOME).setIndicator(TAB_HOME);
32         ts1.setContent(new Intent(MainActivity.this,FirstActivity.class));
33         mth.addTab(ts1);
34 
35         TabSpec ts2=mth.newTabSpec(TAB_MSG).setIndicator(TAB_MSG);
36         ts2.setContent(new Intent(MainActivity.this,SecondActivity.class));
37         mth.addTab(ts2);
38 
39 
40         TabSpec ts3=mth.newTabSpec(TAB_ABT).setIndicator(TAB_ABT);
41         ts3.setContent(new Intent(MainActivity.this,ThirdActivity.class));
42         mth.addTab(ts3);
43 
44         TabSpec ts4=mth.newTabSpec(TAB_SEH).setIndicator(TAB_SEH);
45         ts4.setContent(new Intent(MainActivity.this,ForthActivity.class));
46         mth.addTab(ts4);
47 
48         TabSpec ts5=mth.newTabSpec(TAB_MORE).setIndicator(TAB_MORE);
49         ts5.setContent(new Intent(MainActivity.this,FifthActivity.class));
50         mth.addTab(ts5);
51 
52         this.radioGroup=(RadioGroup)findViewById(R.id.main_radio);
53         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
54 
55                 @Override
56                 public void onCheckedChanged(RadioGroup group, int checkedId) {
57                     // TODO Auto-generated method stub
58 
59                     switch(checkedId){
60                         case R.id.radio_button0:
61 
62                             mth.setCurrentTabByTag(TAB_HOME);
63                             break;
64                         case R.id.radio_button1:
65 
66                             mth.setCurrentTabByTag(TAB_MSG);
67                             break;
68                         case R.id.radio_button2:
69                             mth.setCurrentTabByTag(TAB_ABT);
70                             break;
71                         case R.id.radio_button3:
72 
73                             mth.setCurrentTabByTag(TAB_SEH);
74                             break;
75                         case R.id.radio_button4:
76 
77                             mth.setCurrentTabByTag(TAB_MORE);
78                             break;
79                     }
80                 }
81             });
82 
83     }
84 }

以上兩種寫法熟優熟劣?

安卓初級教程(5):TabHost的思考

标签:

原文地址:http://www.cnblogs.com/kanchihong/p/4657688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!