android用戶(hù)界面框架采用什么模式
ListPrefere(?⊿?)nce簡(jiǎn)介
Li??stPreference是戶(hù)界Android中的一種首選項框架,它??允許用戶(hù)從一個(gè)列表中選擇一個(gè)選項,面框模式ListPreference通常用于那些只有有限個(gè)選項的架采情況,例如顏色、用用字體大小等,戶(hù)界與Spinner相比,??面框模式ListPreference的架采性能更好,因為它不(bu)需要在內存中存儲所有的用用選??項數據。
ListPreference的戶(hù)界使用
1、創(chuàng )建ListPreference
要使用ListPreference,面框模式首先需要在布局文件中添加一個(gè)EditText(╯°□°)╯︵ ┻━┻控件,用于輸入用戶(hù)的架采選擇,然后ヽ(′ー`)ノ在代碼??中創(chuàng )建一個(gè)(′▽?zhuān)?)ListPreference實(shí)例,用用并將其與EditText控件關(guān)聯(lián)。戶(hù)界
<androidx.preference.ListPreference android:id="@+id/list_preference" android:layou(′?`)t_width="match??_parent" android:layout_height="wrap_content" android:entries="@array/coloヽ(′?`)ノrs" android:entryValues="@array/color_codes" app:dialogTheme=&(╬?益?)quot;@style/DialogTheme" />
2、面(mian)框模式在Java代碼中設置ListPr??(′?_?`)eference的(′?`*)監聽(tīng)器
要監聽(tīng)ListPreference的變化,需要為其(qi)設置一個(gè)OnPreferenceChangeListener,當用戶(hù)更改首選項時(shí),監聽(tīng)器會(huì )被調用,并傳入用戶(hù)選擇的新值。
ListPreference listPreference = (ListPreference) findPref???erence(???"list_preference");listPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenc??eChange(Preference preference, Object newValue) { String selected='selected'Color = (String) ne?wValue; // 根據用戶(hù)選擇的顏色執行相應操作 return true; }});3、從資源文??件中獲取列表項數據
要讓ListPreference顯示一個(gè)列表,需??要在資源文件(如resヽ(′ー`)ノ/values/strings.xml)中定義一個(gè)字符串數組,其中包含列表項的文本,然后在代碼中通過(guò)ArrayUtils類(lèi)將數組轉換為CharSequence數組。
<resources> <string-array name="colors"> &l??t;item>紅色</item> <item>綠色<(╯‵□′)╯/item> <item>藍ヽ(′▽?zhuān)?ノ色</item> </string-array>ヾ(′?`)?</resources>
import androidx.annot(′?_?`)ation.NonNull;import java.util.Arrays;import java.util.List;import android.content.Context;import androidx.core.content.res.Re??sourcesCompat;imp??ort org.apa??che.commons.lang3.ArrayUtils;public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle sav( ???)edInstanceState) { super.onCreate(savedInst??anceState); setContentView(R.layout.activity_main); }}List<CharSequence> colorEntries = Arrays.asList(getResources().getStringArray(R.array.colors));CharSequence[] colorCodes = ArrayUtils.toObject(colorEn???trie??s);
4、為L(cháng)istPreference設置列表項圖標和提示文字樣式
如果希望為列表項添加圖標和自定義提示文字樣式,可以在資源文件中定義(╯°□°)╯︵ ┻━┻一個(gè)drawable和string數組,然后在代碼中通過(guò)TypedArray獲取這些資源ID,最(╯°□°)╯后將這些資源ID設置到ListPreference的entryIcon和entr(???)yHint屬性上。
<r(?_?;)esources> <drawable-array name="color_co??des"> <item>@mipmap/red</item> <??;item(′?`*)>(???);@mip??map/green</item> <item>@mipmap/blue</item> </drawable(′?_?`)-array></resources>
TypedArray typedArray = getResources().obtainTypedArray(R.array.color_codes);int[] colorCodeIds = new int[typedArray.length()];for (int i = 0; i < typedArra??y.length(); i++) { colorCodeIds[i] = typedArray.getResourceId(i, R.mipmap.red); //默認圖標為紅色圓點(diǎn)圖標,如果沒(méi)(mei)有對應的資源ID則使用(yong)默認值,可以自行修改。!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%??%%%%%%%%%%%%%%%%%%%%%%%%&&&&&&&&&&amヽ(′?`)ノp;&&&&&&&&&&&&&&&&(??ヮ?)?*:???amp;&&&&&&&&&&&&&amヽ(′▽?zhuān)?ノp;&&&&&(???);&&&&&(((((((((((((((((((((((ˉ\_(ツ)_/ˉ(((((((((((((((′-ι_-`)((((((((((&&&&&&am??p;&&&&??;&&&&&&??amp;&&&&┐(′д`)┌;&&&&&a(◎_◎;)mp;&??amp;??&&&&&&&ヾ(^-^)ノ;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&(?????)amp;&&&&&&&&&a??mp;&&&&&&&%.....................?................................................??........................(′?`*)...............%^^^^^^^^^^^^^^^^^^^^^^^^^^?^^^^^^^^^^^^^^%
$$}}}}(′?`)}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}ヽ(′ー`)ノ}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}(′▽?zhuān)?}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}??}}}}}}}}$}}}}}}$}}}}}}}}}}$}}}}}}$}}}}}}}}}$}}}}}}}}??}}}}}$}}}}}}$}}}}}}}}}$}}}}}}$}}}}}}$}}}}}}}}}$}}}}}}$}}}}}}$}}}}}}}}}$}}}}}}}}}$}}}}}}}??}}$}}}}|$





