Android Switch Card tabwidget usage Example _android

Source: Internet
Author: User
Tags event listener sqlite database

The example in this article describes the use of the Android switch card tabwidget. Share to everyone for your reference, specific as follows:

The Tab tab is similar to the phone book interface, through multiple tags to switch different content, to achieve this effect, first of all to know Tabhost, it is a container for multiple tab tags, each tab can correspond to their own layout, for example, the phone book in the tab layout is a linear layout

To use Tabhost, you first get the Tabhost object through the Gettabhost method, and then add the TAB key to the Tabhost through the AddTab method, which, of course, will generate an event when you switch, to catch this event, Need to set the Tabactivity event listener Setontabchangedlistener

Here's a small example:

Tabtest.java:

Package org.hualang.tab;
Import android.app.Activity;
Import android.app.TabActivity;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.widget.TabHost;
Import Android.widget.Toast;
Import Android.widget.TabHost.OnTabChangeListener;
  The public class Tabtest extends Tabactivity {/** called ' when the ' activity is ' is a-created-tabhost;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Obtain Tabhost Object tabhost = Gettabhost (); Add tags for tabhost//Create a new Newtabspec (NEWTABSPEC)//Set its label and icon (setindicator)//settings content (setcontent) tabhost.addtab (tab Host.newtabspec ("Tab1"). Setindicator ("tab 1", Getresources (). getdrawable (R.DRAWABLE.IMG1)). SetContent (R.i
    D.TEXT1));
        Tabhost.addtab (Tabhost.newtabspec ("TaB2"). Setindicator ("tab 2", Getresources (). getdrawable (R.DRAWABLE.IMG2))
    . SetContent (R.ID.TEXT2)); Tabhost.addtab (TABHOST.NEWTABspec ("Tab3"). Setindicator ("Tab 3", Getresources (). getdrawable (R.DRAWABLE.IMG3)). SetContent (R.ID.TEXT3));
    Sets the background color of the Tabhost//tabhost.setbackgroundcolor (Color.argb (150,22,70,150));
    Set Tabhost background Image resource Tabhost.setbackgroundresource (R.DRAWABLE.BG0);
    Sets which label is currently displayed Tabhost.setcurrenttab (0); Label Toggle event Handling, Setontabchangedlistener Tabhost.setontabchangedlistener (new Ontabchangelistener () {public void O Ntabchanged (String tabid) {Toast toast=toast.maketext (Getapplicationcontext (), now "+tabid+" label, Toast.leng
        Th_short);
      Toast.show ();
  }
    });

 }
}

Main.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <tabhost xmlns:android= "Http://schemas.android.com/apk/res/android" "Android:id=" @android: Id/tabhost "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "> < LinearLayout android:orientation= "vertical" android:layout_width= fill_parent "android:layout_height=" Fill_par Ent "> <tabwidget android:id=" @android: Id/tabs "android:layout_width=" Fill_parent "Android:layo" ut_height= "Wrap_content"/> <framelayout android:id= "@android: Id/tabcontent" android:layout_width= "f Ill_parent "android:layout_height=" fill_parent "> <textview android:id=" @+id/text1 "Andro" Id:layout_width= "Fill_parent" android:layout_height= fill_parent "android:text=" tab 1 "/> <Text View android:id= "@+id/text2" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" "Android:text=" tab 2 "/> <textview android:id= "@+id/text3" android:layout_width= "Fill_parent" Android:layout_

 height= "fill_parent" android:text= "tab 3"/> </FrameLayout> </LinearLayout> </TabHost>

More interested readers of Android-related content can view the site: "Summary of Android Control usage", "Android View Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced course" and " Android Resource Operating Skills summary

I hope this article will help you with the Android program.

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.