Android pure java write Spinner, Android pure javaspinner
[Java]View plaincopy
- Import android. app. Activity;
- Import android. OS. Bundle;
- Import android. view. View;
- Import android. widget .*;
- Import android. widget. AdapterView. OnItemSelectedListener;
- Public class SpinnerTest extends Activity {
- Private LinearLayout mainLayout = null;
- Private TextView tv1 = null;
- Private Spinner spinner1 = null;
- Private TextView tv2 = null;
- Private Spinner spinner2 = null;
- Private TextView tv3 = null;
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- MainLayout_init ();
- SetContentView (mainLayout );
- }
- Void mainLayout_init (){
- MainLayout = new LinearLayout (this );
- LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (-1,-1 );
- MainLayout. setLayoutParams (lp );
- MainLayout. setOrientation (LinearLayout. VERTICAL );
- Tv1_init ();
- MainLayout. addView (tv1 );
- Spinner1_init ();
- MainLayout. addView (spinner1 );
- Tv2_init ();
- MainLayout. addView (tv2 );
- Spinner2_init ();
- MainLayout. addView (spinner2 );
- Tv3 = new TextView (this );
- Tv3.setText ("display ");
- MainLayout. addView (tv3 );
- }
- /* Initialize tv1 */
- Void tv1_init (){
- Tv1 = new TextView (this );
- LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (-1,-2 );
- Tv1.setLayoutParams (lp );
- Tv1.setText ("Spinner test ");
- }
- /* Initialize the spinner */
- Void spinner1_init (){
- Spinner1 = new Spinner (this );
- LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (-1,-2 );
- Spinner1.setLayoutParams (lp );
- Spinner1.setPrompt ("this is the title ");
- String s [] = {"option 1", "option 2", "option 3 "};
- ArrayAdapter <String> aa = new ArrayAdapter <String> (this, android. R. layout. simple_spinner_item, s );
- // Set it to the drop-down Item format
- Aa. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );
- Spinner1.setAdapter (aa );
- // Add a listener
- OnItemSelectedListener otsc = new OnItemSelectedListener (){
- Public void onItemSelected (AdapterView <?> Arg0, View arg1, int arg2, long arg3 ){
- Tv3.setText ("option" + String. valueOf (arg2 + 1) + "selected ");
- }
- Public void onNothingSelected (AdapterView <?> Arg0 ){
- }
- };
- Spinner1.setOnItemSelectedListener (otsc );
- }
- /**/
- Void tv2_init (){
- Tv2 = new TextView (this );
- LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (-1,-2 );
- Tv2.setLayoutParams (lp );
- Tv2.setText ("Change style ");
- }
- /**/
- Void spinner2_init (){
- //
- Spinner2 = new Spinner (this );
- LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (-1,-2 );
- Spinner2.setLayoutParams (lp );
- // Spinner2.setPrompt ("this is the title ");
- String s [] = {"option 1", "option 2", "option 3 "};
- ArrayAdapter <String> aa = new ArrayAdapter <String> (this, android. R. layout. simple_spinner_item, s );
- // Another style
- Aa. setDropDownViewResource (android. R. layout. simple_spinner_item );
- Spinner2.setAdapter (aa );
- }
- }