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

 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 }

以上兩種寫法熟優熟劣?

时间: 2024-10-18 17:48:10

安卓初級教程(5):TabHost的思考的相关文章

安卓初級教程(2):SD創建file,儲存與讀寫的方法(1)

package com.sdmadik; import java.io.*; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.view.View.OnClickListener; import android.widget.*; public class FileUse extends Activity { // GUI controls EditText t

安卓初級教程(4):sqlite建立資料庫

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package net.macdidi.myandroidtutorial; import android.content.Context; import android.database.sqlite.SQLiteDatabase;

安卓初級教程(3):ContentProvider的運用原理

1 package com.example.android.provider; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Map; 6 import android.app.Activity; 7 import android.content.ContentValues; 8 import android.database.Cursor; 9 import android.net.

安卓初級教程(1):@Database(1)

1 package com.example.android.db01; 2 3 import android.app.Activity; 4 import android.content.ContentValues; 5 import android.content.Context; 6 import android.database.Cursor; 7 import android.database.sqlite.SQLiteDatabase; 8 import android.databas

安卓中級教程(1):@InjectView

1 package com.mycompany.hungry; 2 3 import android.annotation.SuppressLint; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import a

安卓中級教程(7):annotation中的 public @interface的用法

1 package com.example.ele_me.util; 2 3 import java.lang.annotation.Retention; 4 import java.lang.annotation.Target; 5 import java.lang.annotation.ElementType;  6 import java.lang.annotation.RetentionPolicy;  7 8 /** 9 * Use this annotation to mark th

安卓中級教程(2):@InjectView中的對象inject

1 package com.example.ele_me.util; 2 3 import java.lang.annotation.Annotation; 4 import java.lang.reflect.Field; 5 6 import android.app.Activity; 7 8 /** 9 * Very lightweight form of injection, inspired by RoboGuice, for injecting common ui elements.

安卓中級教程(4):ScrollView與ListView之間的高度問題

在scrollView中加插ListView是一個大難題.其中一個難題是Listview的高度難以計算,輸出效果往往強差人意,就讓我們看看當中的問題 . <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pa

安卓中級教程(7):pathbutton中的animation.java研究

src/geniuz/myPathbutton/myAnimations.java package geniuz.myPathbutton; import java.util.ArrayList; import java.util.List; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.Animator.AnimatorListener; import com.nineol