講了兩種方式,你是否發覺它們的代碼耦合性太高,如果代碼過多,那就是密密麻麻的一大堆,不僅可讀性差,修改維護還很困難。這裡講到的方式三,能夠很好的解決這個緊耦合問題。因為它的布局檔案和各塊代碼都是獨立的檔案。那步入主題吧。
三、TabActivity和TabHost的結合(三)
實現描述:
1.主類繼承TabActivity
public class Tagpage extends TabActivity
2.擷取當前TabHost對象
final TabHost tabHost = getTabHost();
3.添加Tab分頁標籤,這裡就是關鍵,把每個分頁面連結成Activity。頁面的跳轉,即是Activity的跳轉。
tabHost.addTab(tabHost.newTabSpec("Tab1")
.setIndicator("tab2", getResources().getDrawable(R.drawable.a1))
.setContent(new Intent(this, Page1.class)));
.............
1、布局檔案
page1.xml
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="This is Tab1"<br /> /><br /></LinearLayout><br />
page3.xml
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="This is Tab3"<br /> /><br /></LinearLayout><br />
page2.xml
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout<br />android:id="@+id/widget30"<br />android:layout_width="fill_parent"<br />android:layout_height="fill_parent"<br />xmlns:android="http://schemas.android.com/apk/res/android"<br />android:orientation="vertical"<br />><br /><EditText<br />android:id="@+id/et_text"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:text="EditText"<br />android:textSize="18sp"<br />><br /></EditText><br /><Button<br />android:id="@+id/bt_show"<br />android:layout_width="149px"<br />android:layout_height="wrap_content"<br />android:text="顯示"<br />><br /></Button><br /></LinearLayout><br />
2、代碼
主代碼:Tagpage.java
package com.myandroid.test;</p><p>import android.app.Activity;<br />import android.app.TabActivity;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.widget.TabHost;</p><p>public class Tagpage extends TabActivity{<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> //setContentView(R.layout.main);<br /> final TabHost tabHost = getTabHost();<br /> tabHost.addTab(tabHost.newTabSpec("Tab1")<br /> .setIndicator("tab2", getResources().getDrawable(R.drawable.a1))<br /> .setContent(new Intent(this, Page1.class)));<br /> tabHost.addTab(tabHost.newTabSpec("Tab2")<br /> .setIndicator("tab2", getResources().getDrawable(R.drawable.a2))<br /> .setContent(new Intent(this, Page2.class)));<br /> // .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); //添加這句話,會使得每次跳轉到該頁面都是建立一個頁面,以往的資料狀態會丟失,讀者自己可以實驗下<br /> tabHost.addTab(tabHost.newTabSpec("Tab3")<br /> .setIndicator("tab2", getResources().getDrawable(R.drawable.a3))<br /> .setContent(new Intent(this, Page3.class))); </p><p> }<br />}
分頁一Activity:Page1.java
package com.myandroid.test;</p><p>import android.app.Activity;<br />import android.os.Bundle;</p><p>public class Page1 extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.page1);<br /> }<br />}
分頁三Activity:Page3.java
package com.myandroid.test;</p><p>import android.app.Activity;<br />import android.os.Bundle;</p><p>public class Page3 extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.page3);<br /> }<br />}
分頁二Activity:Page2.java
package com.myandroid.test;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.EditText;<br />import android.widget.Toast;</p><p>public class Page2 extends Activity {<br />private Button bt_show;<br />private EditText et_text;</p><p> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.page2);</p><p> bt_show = (Button)findViewById(R.id.bt_show);<br /> et_text = (EditText)findViewById(R.id.et_text);<br /> bt_show.setOnClickListener(new ClickEvent());<br /> }</p><p> class ClickEvent implements OnClickListener{</p><p>@Override<br />public void onClick(View v) {<br />// TODO Auto-generated method stub<br />Toast.makeText(Page2.this, et_text.getText(), Toast.LENGTH_SHORT).show();<br />}</p><p> }<br />}
最後,別忘了在AndroidManifest.xml檔案註冊上面用到三個子頁面Activity,如下:
<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.myandroid.test"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br />..................<br /> <!-- 要添加Activity的聲明,否則系統找不到響應的Activity,會報錯 --><br /><activity android:name="Page1"></activity><br /><activity android:name="Page2"></activity><br /><activity android:name="Page3"></activity><br /> </application><br /></manifest>
介紹完了TabActivity和TabHost的結合分頁,下一篇將介紹ActivityGroup + GridView結合實現Tap分頁。