Android Spinner(下拉菜单)的应用举例


AppMain.java

  1. package lxy.litsoft;  
  2.   
  3. import Android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.AdapterView;  
  8. import android.widget.AdapterView.OnItemSelectedListener;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.Spinner;  
  11. import android.widget.Toast;  
  12.   
  13. public class AppMain extends Activity {  
  14.     Spinner s;  
  15.     ArrayAdapter<CharSequence> adapter;  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         adapter = ArrayAdapter.createFromResource(this, R.array.place, android.R.layout.simple_spinner_item);  
  21.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  22.           
  23.         s = (Spinner)findViewById(R.id.spinner);  
  24.         s.setAdapter(adapter);  
  25.         s.setOnItemSelectedListener(new SpinnerListener());  
  26.         s.setPrompt("地点");  
  27.     }  
  28.       
  29.     class SpinnerListener implements OnItemSelectedListener{  
  30.         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
  31.                 long arg3) {  
  32.             String selected = arg0.getItemAtPosition(arg2).toString();  
  33.             Toast.makeText(AppMain.this"what you selected is :"+selected, Toast.LENGTH_LONG).show();  
  34.             Log.d("test""what you selected is :"+selected);  
  35.         }  
  36.   
  37.         public void onNothingSelected(AdapterView<?> arg0) {  
  38.             Toast.makeText(AppMain.this"you have selected nothing", Toast.LENGTH_LONG).show();  
  39.             Log.d("test""you have selected nothing");  
  40.         }  
  41.           
  42.     }  
  43. }  
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12.       
  13. <Spinner  
  14.     android:id="@+id/spinner"  
  15.     android:layout_width="fill_parent"   
  16.     android:layout_height="wrap_content"></Spinner>  
  17.        
  18. </LinearLayout>  
strings.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, AppMain!</string>  
  4.     <string name="app_name">Spinner</string>  
  5.     <string-array name="place">  
  6.         <item>北京</item>  
  7.         <item>河北</item>  
  8.         <item>石家庄</item>  
  9.         <item>邢台</item>  
  10.         <item>广宗</item>  
  11.         <item>小庄</item>  
  12.     </string-array>  
  13. </resources>  
  • 1
  • 2
  • 下一页
【内容导航】
第1页:静态实现 第2页:动态实现

相关内容