Android application Development Details (eight)---------------three Ways to use tab tab for common components (cont.)

Source: Internet
Author: User

Feng Philippine remarried # After many years, if you "not married", if I "not married", we can still together?

To use tabs:

(1) Use Framelayout to List tab components and tab content components in layout files

(2) Activity to inherit Tabactivity

(3) Call Tabactivity's Gettabhost () method

(4) Create tab options with Tabhost

First, the Simple tab

1. Engineering Catalogue


2, Test_tab_activity.java

Package Com.example.test_tab;import Android.app.tabactivity;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.tabhost;import Android.widget.tabhost.ontabchangelistener;import Android.widget.TextView;import Android.widget.toast;public class Test_tab_activity extends tabactivity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate);//should not be added setcontentview ()//Get Tabhost,newtabspec () Create a new tabfinal tabhost tab = Gettabhost (); Layoutinflater.from (This). Inflate (R.layout.test__tab,tab.gettabcontentview (), true); Tab.addtab (Tab.newTabSpec (" All "). Setindicator (" All call Logs "). SetContent (r.id.textview01)); Tab.addtab (Tab.newtabspec (" OK "). Setindicator (" Received calls ") . SetContent (R.ID.TEXTVIEW02)); Tab.addtab (Tab.newtabspec ("Cancel"). Setindicator ("Missed Call"). SetContent ( R.ID.TEXTVIEW03));//The event is only triggered when the Tab tab contents change Tab.setontabchangedlistener (New Ontabchangelistener () {@Overridepublic void ONtabchanged (String arg0) {//TODO auto-generated method StubSystem.out.println ("Current selection:" + Tab.getcurrenttabtag ())});}} 

3. layout file

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:id= "@+id/framelayout01"    android:layout_width= "wrap_content"    android:layout_height= "wrap_content" >    <tabhost        Android:id= "@+id/tabhost01"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content" >    </TabHost>    <textview        android:id= "@+id/textview01"        android:layout_width= "Wrap_ Content "        android:layout_height=" wrap_content "        android:text=" all call logs ok "/>    <textview        Android:id= "@+id/textview02"        android:layout_width= "wrap_content"        android:layout_height= "wrap_content "        android:text=" has received the call OK "/>    <textview        android:id=" @+id/textview03 "        android:layout_ Width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "Missed call OK"/></framelayout >

"Knowledge Point" Setontabchangedlistener () method

4. Results presentation



Second, through the implementation of Interface Tabhost.tabcontentfactory Createtabcontent () method to achieve

1. Engineering Catalogue


2, Test_tab_activity.java

Package Com.example.test_tab2;import Java.util.arraylist;import Java.util.list;import android.app.TabActivity; Import Android.os.bundle;import android.view.menu;import android.view.view;import android.widget.ArrayAdapter; Import Android.widget.listview;import Android.widget.tabhost;import android.widget.TabHost.TabContentFactory; public class Test_tab_activity extends Tabactivity implements tabcontentfactory {@Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Tabhost tab2 = Gettabhost () tab2.addtab (Tab2.newtabspec ("All"). Setindicator ("All call Logs"). SetContent (this)); Tab2.addtab (Tab2.newtabspec ("OK"). Setindicator ("Received call"). SetContent (this)); Tab2.addtab (Tab2.newtabspec ("Cancel") . Setindicator ("Missed calls"). SetContent (this));} Public View createtabcontent (String tag) {ListView LV = new ListView (this); list<string> list = new arraylist<string> ();//List.add (TAG), if (Tag.equals ("All")) {List.add ("Tom"); List.add ("Kite"); List.add ("Rose"); else if (tag.eqUals ("OK")) {List.add ("Tom"); List.add ("Kite");} else {List.add ("Rose");} Arrayadapter adapter = new Arrayadapter (this,android. r.layout.simple_list_item_checked, list); Lv.setadapter (adapter); return LV;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (r.menu.test__tab_, menu); return true;}}

3. layout file Test_tab.xml

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:id= "@+id/framelayout01"    android:layout_width= "wrap_content"    android:layout_height= "wrap_content" >    <tabhost        Android:id= "@+id/tabhost01"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content" >    </TabHost></FrameLayout>

4. Results presentation


Third, through SetContent (Intent i) to pass the Intent object, to add content

1. Engineering Catalogue


2, Test_tab_activity.java, also used to Activity1.java, Activity2.java, Activity3.java

Package Com.example.test_tab3;import Android.os.bundle;import Android.app.activity;import android.app.TabActivity; Import Android.content.intent;import Android.view.menu;import Android.widget.tabhost;public class Test_Tab_Activity    Extends Tabactivity {private Intent intent1,intent2,intent3;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Intent1 = new Intent (Getapplicationcontext (), activity1.class);        Intent2 = new Intent (Getapplicationcontext (), activity2.class);        Intent3 = new Intent (Getapplicationcontext (), activity3.class); Tabhost tab3 = Gettabhost () tab3.addtab (Tab3.newtabspec ("All"). Setindicator ("All call Logs"). SetContent (Intent1)); Tab3.addtab (Tab3.newtabspec ("OK"). Setindicator ("Received call"). SetContent (Intent2)); Tab3.addtab (Tab3.newtabspec ("    Cancel "). Setindicator (" Missed Call "). SetContent (Intent3)); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items toThe Action Bar if it is present.        Getmenuinflater (). Inflate (r.menu.test__tab_, menu);    return true; }    }


Package Com.example.test_tab3;import Android.app.activity;import Android.os.bundle;public class Activity1 extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity1);}}


3, layout file test_tab.xml, there are corresponding layout files Activity1.xml, Activity2.xml, Activity3.xml

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:id= "@+id/framelayout01"    android:layout_width= "wrap_content"    android:layout_height= "wrap_content" >    <tabhost        Android:id= "@+id/tabhost01"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content" >    </TabHost></FrameLayout>


<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <textview        android:id= "@+id/textview01" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "        android:text=" all call logs "/></linearlayout>


4. Results presentation


"Note" Don't forget to register Activity1, 2, 3 in Androidmenifest.xml.

Android application Development Details (eight)---------------three Ways to use tab tab for common components (cont.)

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.