Android getting started tutorial (22) -- tabhost, tabwidget)

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

This time we will introduce the tab control of Android. The tab control can achieve the paging effect, so that the content of a screen can be as rich as possible. Of course, it will also increase the complexity of development, use it again when necessary. The tab control of Android is a bit strange to use. It must contain and follow the following sequence:

Tabhost control-> tabwidget (must be named tabs)-> framelayout (must be named tabcontent ).

 

Next, paste the example running:

 

Source code of Main. xml:

View plain Copy to clipboard Print ?
  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Tabhost Android: layout_width="Fill_parent"
  3. Android: layout_height="Fill_parent" Xmlns: Android=Http://schemas.android.com/apk/res/android" Android: ID="@ Android: ID/tabhost1">
  4. <Tabwidget Android: ID="@ Android: ID/tabs"
  5. Android: layout_height="Wrap_content" Android: layout_width="Fill_parent">
  6. </Tabwidget>
  7. <Framelayout Android: ID="@ Android: ID/tabcontent"
  8. Android: paddingtop = "65px" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
  9. linearlayout Android: layout_height = "wrap_content" Android: ID = "@ + ID/tab1" Android: orientation = "vertical" Android: layout_width = "fill_parent"
  10. edittext Android: layout_height = "wrap_content" Android: ID = "@ + ID/edttab1" Android: layout_width = "fill_parent" edittext >
  11. button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: ID = "@ + ID/btntab1" Android: text = "tab1" >< /span> button >
  12. </Linearlayout>
  13. <Linearlayout Android: layout_height="Wrap_content" Android: ID="@ + ID/tab2" Android: layout_width="Fill_parent" Android: Orientation="Horizontal">
  14. edittext Android: layout_height = "wrap_content" Android: ID = "@ + ID/edttab2" Android: layout_width = "wrap_content" Android: layout_weight = "300" edittext >
  15. button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: ID = "@ + ID/btntab2" Android: text = "tab2" >< /span> button > linearlayout >
  16. </Framelayout>
  17. </Tabhost>

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <tabhost Android: layout_width = "fill_parent" <br/> Android: layout_width = "fill_parent" xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ Android: ID/tabhost1 "> <br/> <tabwidget Android: Id =" @ Android: ID/tabs "<br/> Android: layout_height =" wrap_content "Android: layout_width = "fill_parent"> <br/> </tabwidget> <br/> <framelayout Android: Id = "@ Android: ID/tabcontent" <br/> Android: paddingtop = "65px" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <br/> <linearlayout Android: layout_height = "wrap_content" Android: id = "@ + ID/tab1" Android: Orientation = "vertical" Android: layout_width = "fill_parent"> <br/> <edittext Android: layout_height = "wrap_content" Android: id = "@ + ID/edttab1" Android: layout_width = "fill_parent"> </edittext> <br/> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Id = "@ + ID/btntab1" Android: TEXT = "tab1"> </button> <br/> </linearlayout> <br/> <linearlayout Android: layout_height = "wrap_content" Android: id = "@ + ID/tab2" Android: layout_width = "fill_parent" Android: Orientation = "horizontal"> <br/> <edittext Android: layout_height = "wrap_content" Android: id = "@ + ID/edttab2" Android: layout_width = "wrap_content" Android: layout_weight = "300"> </edittext> <br/> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Id = "@ + ID/btntab2" Android: TEXT = "tab2"> </button> </linearlayout> <br/> </framelayout> <br/> </tabhost>

 

 

ProgramSource code:

View plain Copy to clipboard Print ?
  1. PackageCom. testtab;
  2. ImportAndroid. App. tabactivity;
  3. ImportAndroid. OS. Bundle;
  4. ImportAndroid. View. view;
  5. ImportAndroid. widget. Button;
  6. ImportAndroid. widget. edittext;
  7. ImportAndroid. widget. tabhost;
  8. ImportAndroid. widget. tabhost. tabspec;
  9. Public ClassTesttabExtendsTabactivity {// Tabactivity-based construction
  10. Button btntab1, btntab2;
  11. Edittext edttab1, edttab2;
  12. /** Called when the activity is first created .*/
  13. @ Override
  14. Public VoidOncreate (bundle savedinstancestate ){
  15. Super. Oncreate (savedinstancestate );
  16. Setcontentview (R. layout. Main );
  17. Tabhost tabs = gettabhost ();
  18. // Set tab1
  19. Tabspec tab1 = tabs. newtabspec ("Tab1");
  20. Tab1.setindicator ("Tab1");// Set the tab1 name
  21. Tab1.setcontent (R. Id. tab1 );// Associate controls
  22. Tabs. addtab (tab1 );// Add tab1
  23. Btntab1 = (button)This. Findviewbyid (R. Id. btntab1 );
  24. Edttab1 = (edittext)This. Findviewbyid (R. Id. edttab1 );
  25. Btntab1.setonclicklistener (NewClickevent ());
  26. // Set tab2
  27. Tabspec tab2 = tabs. newtabspec ("Tab2");
  28. Tab2.setindicator ("Tab2");
  29. Tab2.setcontent (R. Id. tab2 );
  30. Tabs. addtab (tab2 );
  31. Btntab2 = (button)This. Findviewbyid (R. Id. btntab2 );
  32. Edttab2 = (edittext)This. Findviewbyid (R. Id. edttab2 );
  33. Btntab2.setonclicklistener (NewClickevent ());
  34. Tabs. setcurrenttab (0);
  35. }
  36. ClassClickeventImplementsView. onclicklistener {
  37. @ Override
  38. Public VoidOnclick (view v ){
  39. If(V = btntab1)
  40. {
  41. Edttab1.settext ("Tab1");
  42. }
  43. Else If(V = btntab2)
  44. {
  45. Edttab2.settext ("Tab2");
  46. }
  47. }
  48. }
  49. }
Related Article

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.