安卓纯java写Spinner

 技术分享  技术分享

 

技术分享  技术分享

 

[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.   
  7. public class SpinnerTest extends Activity {  
  8.       
  9.     private LinearLayout mainLayout=null;  
  10.     private TextView tv1=null;  
  11.     private Spinner spinner1=null;  
  12.     private TextView tv2=null;  
  13.     private Spinner spinner2=null;  
  14.     private TextView tv3=null;  
  15.       
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         mainLayout_init();  
  19.         setContentView(mainLayout);  
  20.     }  
  21.       
  22.     void mainLayout_init(){  
  23.         mainLayout=new LinearLayout(this);  
  24.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-1);  
  25.         mainLayout.setLayoutParams(lp);  
  26.         mainLayout.setOrientation(LinearLayout.VERTICAL);  
  27.         tv1_init();  
  28.         mainLayout.addView(tv1);  
  29.         spinner1_init();  
  30.         mainLayout.addView(spinner1);  
  31.         tv2_init();  
  32.         mainLayout.addView(tv2);  
  33.         spinner2_init();  
  34.         mainLayout.addView(spinner2);  
  35.         tv3=new TextView(this);  
  36.         tv3.setText("显示");  
  37.         mainLayout.addView(tv3);  
  38.     }  
  39.     /*tv1初始化*/  
  40.     void tv1_init(){  
  41.         tv1=new TextView(this);  
  42.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);  
  43.         tv1.setLayoutParams(lp);  
  44.         tv1.setText("Spinner测试");  
  45.     }  
  46.     /*spinner初始化*/  
  47.     void spinner1_init(){  
  48.         spinner1=new Spinner(this);  
  49.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -2);  
  50.         spinner1.setLayoutParams(lp);  
  51.         spinner1.setPrompt("这是标题");  
  52.         String s[]={"选项一","选项二","选项三"};  
  53.         ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,s);  
  54.         //设置为下拉式Item格式  
  55.         aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  56.         spinner1.setAdapter(aa);  
  57.         //添加监听  
  58.         OnItemSelectedListener otsc=new OnItemSelectedListener(){  
  59.             public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {  
  60.                 tv3.setText("选项"+String.valueOf(arg2+1)+"被你选中了");  
  61.             }  
  62.             public void onNothingSelected(AdapterView<?> arg0) {  
  63.                   
  64.             }  
  65.         };  
  66.         spinner1.setOnItemSelectedListener(otsc);  
  67.     }  
  68.     /**/  
  69.     void tv2_init(){  
  70.         tv2=new TextView(this);  
  71.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);  
  72.         tv2.setLayoutParams(lp);  
  73.         tv2.setText("换一种风格");  
  74.     }  
  75.     /**/  
  76.     void spinner2_init(){  
  77.         //  
  78.         spinner2=new Spinner(this);  
  79.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1, -2);  
  80.         spinner2.setLayoutParams(lp);  
  81.         //spinner2.setPrompt("这是标题");  
  82.         String s[]={"选项一","选项二","选项三"};  
  83.         ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,s);  
  84.         //另一种风格  
  85.         aa.setDropDownViewResource(android.R.layout.simple_spinner_item);  
  86.         spinner2.setAdapter(aa);  
  87.     }  
  88. }  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。