現在的位置: 首頁 > web前端 > 正文

listpreference的使用

2020年07月21日 web前端 ⁄ 共 1401字 ⁄ 字型大小 評論關閉

  listprefenence比switchpreference多了一個arrays.xml,這個arrays.xml就是用來寫我們需要的list的內容。以切換mode功能為例,就是切換協議的mode,一共需要五個選項。除了switchpreference中的key,title,summary和persistent外,還要有entries和entryValues。android:entries設置的內容是我們在設置時看到的內容,而android:entryValues就是保存在preferences.xml中的值。


  listpreference代碼


  <ListPreference


  android:key="frequency_band"


  android:title="@string/wifi_setting_frequency_band_title"


  android:summary="@string/wifi_setting_frequency_band_summary"


  android:persistent="false"


  android:entries="@array/wifi_frequency_band_entries"


  android:entryValues="@array/wifi_frequency_band_values"


  />


  以下面為例,entries就是我們可以看到的,選擇的項:a,b,g,n,ac。在xml中它們分別對應的是0,1,2,3,4。


  <string-arrayname="mode_80211_selection_entries">


  <item>"a"</item>


  <item>"b"</item>


  <item>"g"</item>


  <item>"n"</item>


  <item>"ac"</item>


  </string-array>


  <string-arrayname="mode_80211_selection_values">


  <!--Donottranslate.-->


  <item>0</item>


  <!--Donottranslate.-->


  <item>1</item>


  <!--Donottranslate.-->


  <item>2</item>


  <item>3</item>


  <item>4</item>


  </string-array>


  listpreference怎麼修改


  原來switchpreference切換狀態是通過setChecked函數進行的,而listpreference沒有這個函數,而是通過setSummary和setValueIndex來進行state的修改。setSummary就是來修改listpreference的sunmmary,參數就是一個字元串數組,用來選擇在那個界面的時候顯示list里的哪一項,而setValueIndex的參數是整型的數字,用來控制listpreference顯示指在哪一個選項上,從上到下依次是0、1、2等等。今天在修改時,先用的是listpreference的setValue函數,發現這個函數並不是用來修改指針所指向的位置。


  總之,listpreference給大家簡單的介紹了一些,希望大家多看看。


  

抱歉!評論已關閉.