Implementation of Tab labels for Android: TabActivity and TabHost (2)

Source: Internet
Author: User

In the previous article, we talked about how TabActivity and TabHost work together. Here, we will talk about method 2. In fact, the layout files of method 1 and method 2 are similar, but they can be independent. Of course, there are also some differences. For example, when you click the top Tab, the page Jump Event Response implementation is also different. For details, see the source code.

Ii. Combine TabActivity and TabHost to implement paging labels -------- method 2

Detail analysis:

1. The main class inherits TabActivity

Public class Pagination extends TabActivity

2. Get the current TabHost object

TabHost tabHost = getTabHost ();

3. Add Tab pages
TabHost. addTab (tabHost. newTabSpec ("Tab1 ")

. SetIndicator ("Tab1", getResources (). getDrawable (R. drawable. a1 ))
. SetContent (this ));

........

Here, you may wonder if you do not need to add layout files! Yes, you want to add it, but add it dynamically. That is, when you click the Tab, the corresponding layout file is dynamically added.

Public View createTabContent (String tag ){..........}

1. layout file: secondpage. 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 file:

Package com. myandroid. test; </p> <p> import android. app. tabActivity; <br/> import android. OS. bundle; <br/> import android. util. log; <br/> import android. view. layoutInflater; <br/> import android. view. view; <br/> import android. widget. button; <br/> import android. widget. editText; <br/> import android. widget. tabHost; <br/> import android. widget. textView; <br/> import android. widget. toast; </p> <p> public class TabPage extends TabActivity implements TabHost. tabContentFactory {<br/> @ Override <br/> protected void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); <br/> // setContentView (R. layout. <br/> final TabHost tabHost = getTabHost (); // tab control object <br/> tabHost. addTab (tabHost. newTabSpec ("Tab1") // Add a tab on the top <br/>. setIndicator ("Tab1", getResources (). getDrawable (R. drawable. a1) <br/>. setContent (this); <br/> tabHost. addTab (tabHost. newTabSpec ("Tab2") <br/>. setIndicator ("Tab2", getResources (). getDrawable (R. drawable. a2) <br/>. setContent (this); <br/> tabHost. addTab (tabHost. newTabSpec ("Tab3") <br/>. setIndicator ("Tab3", getResources (). getDrawable (R. drawable. a3) <br/>. setContent (this); <br/>}</p> <p>/** <br/> * click the Tab, event handling triggered when a page is redirected <br/> */<br/> public View createTabContent (String tag) {<br/> Log. e ("tag", tag); // The tag string here is tabHost. newTabSpec ("Tab1") defined string <br/> int tabPage = Integer. parseInt (tag. substring (tag. length ()-1); // obtain the last number <br/> final TextView TV = new TextView (this); // use final to modify, otherwise, an error is returned. <br/> TV. setText ("This is" + tag); </p> <p> switch (tabPage) {<br/> case 1: // page 1 <br/> break; </p> <p> case 2: // page 2 <br/> final LayoutInflater layout = LayoutInflater. from (TabPage. this); // The object used to load XML, which must be modified using final <br/> final View customView = layout. inflate (R. layout. secondpage, null); // create a custom View. Use final to modify <br/> final Button bt_show = (Button) customView. findViewById (R. id. bt_show); <br/> final EditText et_text = (EditText) mmview. findViewById (R. id. et_text); <br/> bt_show.setOnClickListener (new Button. onClickListener () {<br/> @ Override <br/> public void onClick (View v) {<br/> // TODO Auto-generated method stub <br/> Toast. makeText (TabPage. this, et_text.getText (), Toast. LENGTH_SHORT ). show (); <br/>}< br/>}); <br/> return customView; </p> <p> case 3: // page 3 <br/> break; <br/> default: <br/> break; <br/>}</p> <p> // null cannot be returned. <br/> return TV; </p> <p >}< br/>

 

In the next article, we will continue to talk about the combination of TabActivity and TabHost to implement pagination tag -------- method 3, which is much better than the first two methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.