I talked about the two methods. Do you realize that their code coupling is too high? If there are too many code, it is a lot of complicated code, which is not only difficult to read, but also difficult to modify and maintain. Method 3 mentioned here can effectively solve this tight coupling problem. Because its layout files and block code are independent files. Enter the topic.
3. Combination of TabActivity and TabHost (3)
Implementation description:
1. The main class inherits TabActivity
Public class Tagpage extends TabActivity
2. Get the current TabHost object
Final TabHost tabHost = getTabHost ();
3. Add a Tab page tag, which is the key here. link each page to an Activity. Page jump, that is, Activity jump.
TabHost. addTab (tabHost. newTabSpec ("Tab1 ")
. SetIndicator ("tab2", getResources (). getDrawable (R. drawable. a1 ))
. SetContent (new Intent (this, Page1.class )));
.............
1. layout File
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/> <TextView <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" <br/> android: text = "This is Tab1" <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/> <TextView <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" <br/> android: text = "This is Tab3" <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/> <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/> </EditText> <br/> <Button <br/> android: id = "@ + id/bt_show" <br/> android: layout_width = "149px" <br/> android: layout_height = "wrap_content" <br/> android: text = "show" <br/> </Button> <br/> </LinearLayout> <br/>
2. Code
Main Code: 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); // when this sentence is added, a new page is created every time you jump to the page, and the previous data status will be lost, you can test <br/> tabHost. addTab (tabHost. newTabSpec ("Tab3") <br/>. setIndicator ("tab2", getResources (). getDrawable (R. drawable. a3) <br/>. setContent (new Intent (this, Page3.class); </p> <p >}< br/>}
Page 1 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/>}
Page 3 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/>}
Page 2 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/>}
Finally, do not forget to use three subpage activities in the AndroidManifest. xml file registration, as shown below:
<? 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/> <! -- The declaration of the Activity to be added; otherwise, the system will report an error if the Activity in response cannot be found --> <br/> <activity android: name = "Page1"> </activity> <br/> <activity android: name = "Page2"> </activity> <br/> <activity android: name = "Page3"> </activity> <br/> </application> <br/> </manifest>
This section describes the combined pagination of TabActivity and TabHost. The next section describes how to implement Tap paging by combining ActivityGroup and GridView.