學習SDK裏的samples--Spinner(下拉列表)

 sdk自帶了很多的samples,學習一下里面的一個Spinner。

一開始不會怎麼導入samples到eclipse中,有2個方法可以導入

file->new->other->Android->Android Sample Project或者Android Project From Existing Code

導入之後先運行看看效果(電腦配置不給力,模擬器啓動不起,只能用真機)

 

選擇了之後

這個程序在退出下次進的時候也可以保持住上次選中的選項,所以需要進行存儲。

學到的知識點:

Spinner控件的基本使用+ArrayAdapter(數組適配器,設置Spinner的data)

  Spinner是神馬ArrayAdapter腫麼用

string.xml定義的數組在代碼裏如何獲取

string.xml如何定義數組

SharedPreferences(輕量級的存儲)的使用

android數據存儲的4種方式

 

模仿samples的Spinner做一個寫個程序

新建一個ChooseFaces的項目

string.xml

  1. <resources> 
  2.  
  3.     <string name="app_name">ChooseFaces</string> 
  4.     <string name="prompt">請選擇表情</string> 
  5.     <string name="lastType">上次退出前選擇的表情是:</string> 
  6.     <string name="labelPref">您選擇的表情是:</string> 
  7.  
  8.     <string-array name="faceType"> 
  9.         <item>泡泡</item> 
  10.         <item>綠豆蛙</item> 
  11.         <item>兔斯基</item> 
  12.         <item>阿狸</item> 
  13.     </string-array> 
  14.  
  15. </resources> 

layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:background="#ffffff" 
  6.     android:orientation="vertical" > 
  7.  
  8.     <TextView 
  9.         android:id="@+id/tvpreTitle" 
  10.         android:layout_width="match_parent" 
  11.         android:layout_height="wrap_content" 
  12.         android:gravity="left" 
  13.         android:textColor="#CCCCCC" /> 
  14.  
  15.     <TextView 
  16.         android:id="@+id/tvTitle" 
  17.         android:layout_width="match_parent" 
  18.         android:layout_height="wrap_content" 
  19.         android:layout_marginBottom="20dp" 
  20.         android:layout_marginTop="20dp" 
  21.         android:gravity="left" /> 
  22.  
  23.     <Spinner 
  24.         android:id="@+id/spFaceType" 
  25.         android:layout_width="wrap_content" 
  26.         android:layout_height="wrap_content" 
  27.         android:layout_gravity="left" 
  28.         android:prompt="@string/prompt" /> 
  29.  
  30.     <LinearLayout 
  31.         android:layout_width="match_parent" 
  32.         android:layout_height="match_parent" 
  33.         android:gravity="center" > 
  34.  
  35.         <ImageView 
  36.             android:id="@+id/ivDisplay" 
  37.             android:layout_width="wrap_content" 
  38.             android:layout_height="wrap_content" 
  39.             android:layout_gravity="center" /> 
  40.     </LinearLayout> 
  41.  
  42. </LinearLayout> 

MainActivity.java

  1. package com.dq.choosefaces; 
  2.  
  3. import android.app.Activity; 
  4. import android.content.Context; 
  5. import android.content.SharedPreferences; 
  6. import android.content.res.Resources; 
  7. import android.os.Bundle; 
  8. import android.view.View; 
  9. import android.widget.AdapterView; 
  10. import android.widget.AdapterView.OnItemSelectedListener; 
  11. import android.widget.ArrayAdapter; 
  12. import android.widget.ImageView; 
  13. import android.widget.Spinner; 
  14. import android.widget.TextView; 
  15.  
  16. public class MainActivity extends Activity { 
  17.  
  18.     // 下拉單默認選中的位置 
  19.     private final static int DEFAULT_POSITION = 0
  20.     // 輕量存儲的文件名 
  21.     private final static String PREFERENCES_FILE = "facePrefs"
  22.     // 下拉單位置在存儲文件中的鍵名 
  23.     private final static String POSITION_KEY = "position"
  24.     private final static String SELECT_TEXT_KEY = "selectText"
  25.  
  26.     // 下拉單選中的位置 
  27.     private int position; 
  28.     // 下拉單選中的文本 
  29.     private String selectText = null
  30.     // 文本顯示的前綴 
  31.     private String labelPrefix = null
  32.     // 上次退出前選擇的表情 
  33.     private String lastTypePrefix = null
  34.     // 一個文本控件 
  35.     private TextView tvPre = null
  36.     private TextView tv = null
  37.     // 一個數組適配器,爲Spinner提供數據,SpinnerAdapter的子類 
  38.     private ArrayAdapter<CharSequence> adapter = null
  39.     // 一個下拉單控件 
  40.     private Spinner sp = null
  41.     // 一個圖片控件 
  42.     private ImageView iv = null
  43.  
  44.     @Override 
  45.     protected void onCreate(Bundle savedInstanceState) { 
  46.         super.onCreate(savedInstanceState); 
  47.         setContentView(R.layout.main); 
  48.  
  49.         // 獲取控件 
  50.         this.tvPre = (TextView) findViewById(R.id.tvpreTitle); 
  51.         this.tv = (TextView) findViewById(R.id.tvTitle); 
  52.         this.sp = (Spinner) findViewById(R.id.spFaceType); 
  53.         this.iv = (ImageView) findViewById(R.id.ivDisplay); 
  54.  
  55.         // 獲取資源 
  56.         Resources res = getResources(); 
  57.  
  58.         // 獲取文本顯示的前綴 
  59.         this.labelPrefix = res.getString(R.string.labelPref); 
  60.         this.lastTypePrefix = res.getString(R.string.lastType); 
  61.  
  62.         // 獲取數組數據 
  63.         String[] data = res.getStringArray(R.array.faceType); 
  64.  
  65.         /* 
  66.          * ArrayAdapter靜態方法獲得一個ArrayAdapter實例 
  67.          * (當前Activity,數組資源ID,下拉單一個item的layout資源ID) 第三參數表示spinner沒有展開前的UI類型 
  68.          * 選擇了android提供的樣式,和當前程序的R不同,這是android.R.layout下的資源 
  69.          * 也可以自己new一個,data表示數據的數組 this.adapter = new 
  70.          * ArrayAdapter<CharSequence>(this,android.R.layout.test_list_item, 
  71.          * data); 
  72.          * 如果在layout裏設置了spinner的屬性 android:entries="@array/faceType" 
  73.          * 那麼就不需要在代碼裏spinner.setAdapter()了,自動設置好了 
  74.          */ 
  75.         this.adapter = ArrayAdapter.createFromResource(this, R.array.faceType, 
  76.                 android.R.layout.test_list_item); 
  77.  
  78.         // 設置spinner展開時的UI類型,如果不設置就默認是spinner不展開時的UI類型 
  79.         this.adapter 
  80.                 .setDropDownViewResource(android.R.layout.simple_list_item_single_choice); 
  81.  
  82.         // 設置spinner的適配器 
  83.         this.sp.setAdapter(this.adapter); 
  84.  
  85.         // 添加spinner item被選擇事件 
  86.         this.sp.setOnItemSelectedListener(new MyItemSelectedListener()); 
  87.  
  88.         // 讀取數據 
  89.         readPrefsState(this); 
  90.  
  91.         // 設置上一次退出時的表情 
  92.         this.tvPre.setText(this.lastTypePrefix + this.selectText); 
  93.  
  94.         // 設置下拉單選中的位置 
  95.         this.sp.setSelection(this.position); 
  96.  
  97.     } 
  98.  
  99.     private class MyItemSelectedListener implements OnItemSelectedListener { 
  100.  
  101.         // 重寫此方法 
  102.         @Override 
  103.         public void onItemSelected(AdapterView<?> parent, View view, 
  104.                 int position, long id) { 
  105.  
  106.             // 設置當前選擇的item位置 
  107.             MainActivity.this.position = position; 
  108.  
  109.             // 設置當前選擇的item文本 
  110.             MainActivity.this.selectText = parent.getItemAtPosition(position) 
  111.                     .toString(); 
  112.             MainActivity.this.tv.setText(labelPrefix + selectText); 
  113.  
  114.             int imgId = 0
  115.             switch (position) { 
  116.             case 0
  117.                 imgId = R.raw.pp; 
  118.                 break
  119.             case 1
  120.                 imgId = R.raw.ldw; 
  121.                 break
  122.             case 2
  123.                 imgId = R.raw.tsj; 
  124.                 break
  125.             case 3
  126.                 imgId = R.raw.al; 
  127.                 break
  128.             } 
  129.  
  130.             // 設置圖片 
  131.             MainActivity.this.iv.setImageResource(imgId); 
  132.         } 
  133.  
  134.         @Override 
  135.         public void onNothingSelected(AdapterView<?> parent) { 
  136.         } 
  137.  
  138.     } 
  139.  
  140.     public boolean readPrefsState(Context c) { 
  141.  
  142.         /* 
  143.          * 當前Activity方法獲得一個SharedPreferences實例 第一個參數是文件名 第二個參數是設置文件的訪問權限,有3個選擇 
  144.          * 1、MODE_PRIVATE(只有調用的程序可以訪問) 2、MODE_WORLD_READABLE(別的程序有讀的權限) 
  145.          * 3、MODE_WORLD_WRITEABLE(別的程序有寫的權限) 但是在API17裏2,3被棄用了,it is dangerous 
  146.          */ 
  147.         SharedPreferences spref = c.getSharedPreferences(PREFERENCES_FILE, 
  148.                 MODE_PRIVATE); 
  149.         // 讀取到POSITION_KEY和SELECT_TEXT_KEY的值,如果不存在此key,則默認爲DEFAULT_POSITION 
  150.         this.position = spref.getInt(POSITION_KEY, DEFAULT_POSITION); 
  151.         this.selectText = spref.getString(SELECT_TEXT_KEY, ""); 
  152.  
  153.         // 包含此key則返回true,反之false 
  154.         return (spref.contains(POSITION_KEY)); 
  155.  
  156.     } 
  157.  
  158.     public boolean writePrefsState(Context c) { 
  159.  
  160.         /* 
  161.          * 當前Activity方法獲得一個SharedPreferences實例 第一個參數是文件名 第二個參數是設置文件的訪問權限,有3個選擇 
  162.          * 1、MODE_PRIVATE(只有調用的程序可以訪問) 2、MODE_WORLD_READABLE(別的程序有讀的權限) 
  163.          * 3、MODE_WORLD_WRITEABLE(別的程序有寫的權限) 
  164.          */ 
  165.         SharedPreferences spref = c.getSharedPreferences(PREFERENCES_FILE, 
  166.                 MODE_PRIVATE); 
  167.  
  168.         /* 
  169.          * 獲取SharedPreferences.Editor對象,SharedPreferences本來只可以讀取數據, 
  170.          * 如果要編輯就要獲取Editor對象 
  171.          */ 
  172.         SharedPreferences.Editor edit = spref.edit(); 
  173.  
  174.         // 存儲這2個值 
  175.         edit.putInt(POSITION_KEY, this.position); 
  176.         edit.putString(SELECT_TEXT_KEY, this.selectText); 
  177.  
  178.         // 使用edit.commit()提交,這樣纔會真正存儲到文件中,true表示成功,false表示失敗 
  179.         return (edit.commit()); 
  180.     } 
  181.  
  182.     @Override 
  183.     protected void onStop() { 
  184.  
  185.         // 此Activity停止的時候存儲數據 
  186.         writePrefsState(this); 
  187.  
  188.         super.onStop(); 
  189.     } 
  190.  

存放圖片的目錄raw,一開始可能不存在,需要自己新建一個,然後去百度貼吧拷幾張表情來。

關於SharedPreferences操作的文件存儲在哪裏,可以打開DDMS->File Explorer

展開到/data/data/<package name>/shared_prefs下

我是用真機測試不知道爲何打不開這個目錄,可能是沒有權限訪問的原因,但是我試了在模擬器是可以看到的。

然後就運行一下。

Spinner選擇的對話框

然後關掉程序再打開

陽臺還是挺涼快的 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章