Android之Spinner用法

转自:http://blog.csdn.net/gjanyanlig/article/details/6928975


一. 简单认识

Activity

[java]  view plain copy
  1. package cn.lyj.android;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.SharedPreferences;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.AdapterView.OnItemSelectedListener;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.Spinner;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. public class AndroidUIActivity extends Activity {  
  16.   
  17.     protected int mPos;  
  18.     protected String mSelection;  
  19.     protected ArrayAdapter<CharSequence> mAdapter;  
  20.     public static final int DEFAULT_POSITION = 2;  
  21.     public static final String PREFERENCES_FILE = "SpinnerPrefs";  
  22.     public static final String PROPERTY_DELIMITER = "=";  
  23.     public static final String POSITION_KEY = "Position";  
  24.     public static final String SELECTION_KEY = "Selection";  
  25.     public static final String POSITION_MARKER = POSITION_KEY  
  26.             + PROPERTY_DELIMITER;  
  27.     public static final String SELECTION_MARKER = SELECTION_KEY  
  28.             + PROPERTY_DELIMITER;  
  29.   
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.main);  
  34.   
  35.         Spinner spinner = (Spinner) findViewById(R.id.Spinner01);  
  36.   
  37.         // 设置ArrayAdapter  
  38.         this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,  
  39.                 android.R.layout.simple_spinner_dropdown_item);  
  40.         spinner.setAdapter(this.mAdapter);  
  41.   
  42.         // 选中触发事件,在窗口中央显示项目  
  43.         OnItemSelectedListener spinnerListener = new myOnItemSelectedListener(  
  44.                 thisthis.mAdapter);  
  45.         spinner.setOnItemSelectedListener(spinnerListener);  
  46.     }  
  47.   
  48.     /** 
  49.      * 用户选中下拉列表中得一项 
  50.      */  
  51.     public class myOnItemSelectedListener implements OnItemSelectedListener {  
  52.         ArrayAdapter<CharSequence> mLocalAdapter;  
  53.         Activity mLocalContext;  
  54.   
  55.         // 构造方法  
  56.         public myOnItemSelectedListener(Activity c,  
  57.                 ArrayAdapter<CharSequence> ad) {  
  58.   
  59.             this.mLocalContext = c;  
  60.             this.mLocalAdapter = ad;  
  61.   
  62.         }  
  63.   
  64.         //When the user selects an item in the spinner, this method is invoked by the callback chain  
  65.         public void onItemSelected(AdapterView<?> parent, View v, int pos,  
  66.                 long row) {  
  67.               
  68.             //暂时没发现有什么用  
  69.             //AndroidUIActivity.this.mPos = pos;  
  70.             AndroidUIActivity.this.mSelection = parent.getItemAtPosition(pos).toString();  
  71.   
  72.             TextView resultText = (TextView) findViewById(R.id.SpinnerResult);  
  73.             resultText.setText(AndroidUIActivity.this.mSelection);  
  74.         }  
  75.   
  76.         //无用  
  77.         public void onNothingSelected(AdapterView<?> parent) {  
  78.         }  
  79.     }  
  80.   
  81.     /* 
  82.      * Restores the current state of the spinner (which item is selected, and the value 
  83.      * of that item). 
  84.      */  
  85.     @Override  
  86.     public void onResume() {  
  87.         super.onResume();  
  88.   
  89.         if (!readInstanceState(this))  
  90.             setInitialState();  
  91.   
  92.         Spinner restoreSpinner = (Spinner) findViewById(R.id.Spinner01);  
  93.         restoreSpinner.setSelection(getSpinnerPosition());  
  94.     }  
  95.       
  96.     /** 
  97.      * Read the previous state of the spinner from the preferences file 
  98.      */  
  99.     public boolean readInstanceState(Context c) {  
  100.         SharedPreferences p = c.getSharedPreferences(PREFERENCES_FILE,  
  101.                 MODE_WORLD_READABLE);  
  102.   
  103.         this.mPos = p.getInt(POSITION_KEY, AndroidUIActivity.DEFAULT_POSITION);  
  104.         this.mSelection = p.getString(SELECTION_KEY, "");  
  105.         return (p.contains(POSITION_KEY));  
  106.   
  107.     }  
  108.   
  109.     /* 
  110.      *Store the current state of the spinner (which item is selected, and the value of that item). 
  111.      */  
  112.     @Override  
  113.     public void onPause() {  
  114.         super.onPause();  
  115.   
  116.         if (!writeInstanceState(this)) {  
  117.             Toast.makeText(this"Failed to write state!", Toast.LENGTH_LONG)  
  118.                     .show();  
  119.         }  
  120.     }  
  121.       
  122.     /** 
  123.      * Write the application's current state to a properties repository. 
  124.      */  
  125.     public boolean writeInstanceState(Context c) {  
  126.   
  127.         SharedPreferences p = c.getSharedPreferences(  
  128.                 AndroidUIActivity.PREFERENCES_FILE, MODE_WORLD_READABLE);  
  129.   
  130.         SharedPreferences.Editor e = p.edit();  
  131.         e.putInt(POSITION_KEY, this.mPos);  
  132.         e.putString(SELECTION_KEY, this.mSelection);  
  133.         return (e.commit());  
  134.   
  135.     }  
  136.   
  137.     /** 
  138.      * Sets the initial state of the spinner when the application is first run. 
  139.      */  
  140.     public void setInitialState() {  
  141.         this.mPos = DEFAULT_POSITION;  
  142.     }  
  143.   
  144.     public int getSpinnerPosition() {  
  145.         return this.mPos;  
  146.     }  
  147.   
  148.     public void setSpinnerPosition(int pos) {  
  149.         this.mPos = pos;  
  150.     }  
  151.   
  152.     public String getSpinnerSelection() {  
  153.         return this.mSelection;  
  154.     }  
  155.   
  156.     public void setSpinnerSelection(String selection) {  
  157.         this.mSelection = selection;  
  158.     }  
  159. }  


main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <!--     Creates a Linear Layout View to contain the spinner 
  4.  -->  
  5. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  6.     android:layout_width="fill_parent"  
  7.     android:layout_height="fill_parent"  
  8.     android:orientation="vertical" >  
  9.   
  10.     <!--  
  11.         Creates a spinner widget called Spinner01, within the Linear Layout  
  12.         The prompt text comes from the string "planet_prompt" in strings.xml  
  13.         Spinner不支持android:drawSelectorOnTop此属性  
  14.     -->  
  15.   
  16.     <Spinner  
  17.         android:id="@+id/Spinner01"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:drawSelectorOnTop="true"       
  21.         android:prompt="@string/planet_prompt" >  
  22.     </Spinner>  
  23.   
  24. <!--         Creates a TextView called SpinnerResult, below the spinner. 
  25.  -->  
  26.   
  27.     <TextView  
  28.         android:id="@+id/SpinnerResult"  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="fill_parent"  
  31.         android:gravity="center"  
  32.         android:text="Result"  
  33.         android:textSize="10pt"  
  34.         android:textStyle="bold" >  
  35.     </TextView>  
  36.   
  37. </LinearLayout>  

strings.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <!--  
  4.     The string named "app_name" defines the application's visible name.  
  5.     The string array "Planets" defines an array of 9 strings. The application loads  
  6.     this array into the spinner's array adapter.  
  7.     The string "planet_prompt" defines the prompt for the result text box.  
  8.  -->  
  9. <resources>  
  10.     <string name="app_name">Spinner</string>  
  11.     <string-array name="Planets">  
  12.         <item>Mercury</item>  
  13.         <item>Venus</item>  
  14.         <item>Earth</item>  
  15.         <item>Mars</item>  
  16.         <item>Jupiter</item>  
  17.         <item>Saturn</item>  
  18.         <item>Uranus</item>  
  19.         <item>Neptune</item>  
  20.         <item>Pluto</item>  
  21.     </string-array>  
  22.     <string name="planet_prompt">Select a planet</string>  
  23. </resources>  

AndroidManifest.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="cn.lyj.android"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="10" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:label="@string/app_name"  
  14.             android:name=".AndroidUIActivity" >  
  15.             <intent-filter >  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.   
  23. </manifest>  

二. 运行结果

启动



点击菜单


选择Pluto

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值