Android pure java write Spinner, Android pure javaspinner

Source: Internet
Author: User

Android pure java write Spinner, Android pure javaspinner

 

 

[Java]View plaincopy
  1. Import android. app. Activity;
  2. Import android. OS. Bundle;
  3. Import android. view. View;
  4. Import android. widget .*;
  5. Import android. widget. AdapterView. OnItemSelectedListener;
  6. Public class SpinnerTest extends Activity {
  7. Private LinearLayout mainLayout = null;
  8. Private TextView tv1 = null;
  9. Private Spinner spinner1 = null;
  10. Private TextView tv2 = null;
  11. Private Spinner spinner2 = null;
  12. Private TextView tv3 = null;
  13. Public void onCreate (Bundle savedInstanceState ){
  14. Super. onCreate (savedInstanceState );
  15. MainLayout_init ();
  16. SetContentView (mainLayout );
  17. }
  18. Void mainLayout_init (){
  19. MainLayout = new LinearLayout (this );
  20. LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (-1,-1 );
  21. MainLayout. setLayoutParams (lp );
  22. MainLayout. setOrientation (LinearLayout. VERTICAL );
  23. Tv1_init ();
  24. MainLayout. addView (tv1 );
  25. Spinner1_init ();
  26. MainLayout. addView (spinner1 );
  27. Tv2_init ();
  28. MainLayout. addView (tv2 );
  29. Spinner2_init ();
  30. MainLayout. addView (spinner2 );
  31. Tv3 = new TextView (this );
  32. Tv3.setText ("display ");
  33. MainLayout. addView (tv3 );
  34. }
  35. /* Initialize tv1 */
  36. Void tv1_init (){
  37. Tv1 = new TextView (this );
  38. LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (-1,-2 );
  39. Tv1.setLayoutParams (lp );
  40. Tv1.setText ("Spinner test ");
  41. }
  42. /* Initialize the spinner */
  43. Void spinner1_init (){
  44. Spinner1 = new Spinner (this );
  45. LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (-1,-2 );
  46. Spinner1.setLayoutParams (lp );
  47. Spinner1.setPrompt ("this is the title ");
  48. String s [] = {"option 1", "option 2", "option 3 "};
  49. ArrayAdapter <String> aa = new ArrayAdapter <String> (this, android. R. layout. simple_spinner_item, s );
  50. // Set it to the drop-down Item format
  51. Aa. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );
  52. Spinner1.setAdapter (aa );
  53. // Add a listener
  54. OnItemSelectedListener otsc = new OnItemSelectedListener (){
  55. Public void onItemSelected (AdapterView <?> Arg0, View arg1, int arg2, long arg3 ){
  56. Tv3.setText ("option" + String. valueOf (arg2 + 1) + "selected ");
  57. }
  58. Public void onNothingSelected (AdapterView <?> Arg0 ){
  59. }
  60. };
  61. Spinner1.setOnItemSelectedListener (otsc );
  62. }
  63. /**/
  64. Void tv2_init (){
  65. Tv2 = new TextView (this );
  66. LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (-1,-2 );
  67. Tv2.setLayoutParams (lp );
  68. Tv2.setText ("Change style ");
  69. }
  70. /**/
  71. Void spinner2_init (){
  72. //
  73. Spinner2 = new Spinner (this );
  74. LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (-1,-2 );
  75. Spinner2.setLayoutParams (lp );
  76. // Spinner2.setPrompt ("this is the title ");
  77. String s [] = {"option 1", "option 2", "option 3 "};
  78. ArrayAdapter <String> aa = new ArrayAdapter <String> (this, android. R. layout. simple_spinner_item, s );
  79. // Another style
  80. Aa. setDropDownViewResource (android. R. layout. simple_spinner_item );
  81. Spinner2.setAdapter (aa );
  82. }
  83. }

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.