Tabhost usage Examples

Source: Internet
Author: User

1.mainactivity.java

Key points:

1>Mainactivity extends tabactivity implements Onclicklistener,oncheckedchangelistener

2> Localtabhost.addtab (Buildtabspec ("New", R.string.form_tab_new,
R.drawable.main_tab_bg_selector, New Intent (This,
( hellowordactivity.class)));
3>mR1 = ((RadioButton) Findviewbyid (R.id.radio_button0));
Mr1.setoncheckedchangelistener (this);

4>

<string name= "Form_tab_new" > Latest </string>
<string name= "Form_tab_hot" > Popular </string>
<string name= "Form_tab_get" > Get points </string>
<string name= "Form_tab_soft" > Applications </string>

5>

Main_tab_bg_selector.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:drawable= "@drawable/main_tab_bg" android:state_checked= "true"/>
</selector>

6>

Tab_selector.xml

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:drawable=" @drawable/toptab_normal "android:state_checked=" false "/>

<item android:drawable= "@drawable/toptab_select" android:state_checked= "true"/>

</selector>

7>colors.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>

<color name= "White" > #FFFFFF </color> <!--
<color name= "bg" > #eeeef4 </color>
</resources>

8>toptab_select, Toptab_normal, MAIN_TAB_BG

Package Com.example.tabhostdemo;import Android.os.bundle;import Android.app.activity;import Android.app.tabactivity;import Android.content.intent;import Android.graphics.color;import Android.view.Menu; Import Android.view.view;import Android.view.window;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.compoundbutton;import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.tabhost;import Android.widget.compoundbutton.oncheckedchangelistener;public class Mainactivity extends Tabactivity implements Onclicklistener,oncheckedchangelistener{private Button back;private tabhost mtabhost;private RadioButton mR1;private RadioButton mr2;private RadioButton mr3;private radiogroup main_radio; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); GetWindow (). Requestfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Initview (); Setupintent ();} private void Setupintent () {this.mtabhost = Gettabhost (); Tabhost localtabhost = This.mtabhost;localtabhost.addtab (Buildtabspec ("New", R.string.form_tab_new,r.drawable.main _tab_bg_selector, New Intent (This,hellowordactivity.class)); Localtabhost.addtab (Buildtabspec ("Hot", R.string.form_tab_hot,r.drawable.main_tab_bg_selector, New Intent (This,hellowordactivity2.class)); Localtabhost.addtab (Buildtabspec ("Get", R.string.form_tab_get,r.drawable.main_tab_bg_selector, New Intent (This, Hellowordactivity.class)));} private void Initview () {mR1 = ((RadioButton) Findviewbyid (R.id.radio_button0)); Mr1.setoncheckedchangelistener (This) ; This.mR1.setTextColor (color.red); mR2 = ((RadioButton) Findviewbyid (R.id.radio_button1)); Mr2.setoncheckedchangelistener (this); mR3 = ((RadioButton) Findviewbyid (R.id.radio_button2)); Mr3.setoncheckedchangelistener (this), Main_radio = (radiogroup) Findviewbyid (r.id.main_radio); back = (Button) Findviewbyid (R.id.back); Back.setonclicklistener (this);} Private Tabhost.tabspec Buildtabspec (String tag, int rEslabel, int resicon,final Intent content) {return This.mTabHost.newTabSpec (tag). Setindicator (GetString (Reslabel), Getresources (). getdrawable (Resicon)). SetContent (content);} @Overridepublic void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {if (isChecked) { System.out.println ("isChecked"); switch (Buttonview.getid ()) {case R.ID.RADIO_BUTTON0: This.mTabHost.setCurrentTabByTag ("new"); This.mR1.setTextColor (color.red); This.mR2.setTextColor (Color.Black); This.mR3.setTextColor (Color.Black); Break;case R.id.radio_button1:this.mr1.settextcolor (Color.Black); This.mR3.setTextColor (Color.Black); This.mR2.setTextColor (color.red); This.mTabHost.setCurrentTabByTag ("hot"); Break;case R.id.radio_button2:this.mr1.settextcolor (Color.Black); This.mR2.setTextColor (Color.Black); This.mR3.setTextColor (color.red); This.mTabHost.setCurrentTabByTag ("get"); break;//case r.id.radio_button2://// Mpopupwindow.showasdropdown (main_radio, 0, 0);//Break;}}} @Overridepublic void OnClick (View v) {//TODO Auto-generaTed Method Stubswitch (V.getid ()) {case r.id.back:finish (); break;default:break;}}} 

2.hellowordactivity.java

Like Hellowordactivity2.java and Hellowordactivity.java.

Package Com.example.tabhostdemo;import Android.app.activity;import Android.os.bundle;public class HellowordActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_hello);}}

3.

Tabhost usage Examples

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.