新聞中心
ListPreference簡介
ListPreference是Android中的一種首選項框架,它允許用戶從一個列表中選擇一個選項,ListPreference通常用于那些只有有限個選項的情況,例如顏色、字體大小等,與Spinner相比,ListPreference的性能更好,因為它不需要在內存中存儲所有的選項數(shù)據(jù)。

創(chuàng)新互聯(lián)主要從事網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務興文,10年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220
ListPreference的使用
1、創(chuàng)建ListPreference
要使用ListPreference,首先需要在布局文件中添加一個EditText控件,用于輸入用戶的選擇,然后在代碼中創(chuàng)建一個ListPreference實例,并將其與EditText控件關聯(lián)。
2、在Java代碼中設置ListPreference的監(jiān)聽器
要監(jiān)聽ListPreference的變化,需要為其設置一個OnPreferenceChangeListener,當用戶更改首選項時,監(jiān)聽器會被調用,并傳入用戶選擇的新值。
ListPreference listPreference = (ListPreference) findPreference("list_preference");
listPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String selectedColor = (String) newValue;
// 根據(jù)用戶選擇的顏色執(zhí)行相應操作
return true;
}
});
3、從資源文件中獲取列表項數(shù)據(jù)
要讓ListPreference顯示一個列表,需要在資源文件(如res/values/strings.xml)中定義一個字符串數(shù)組,其中包含列表項的文本,然后在代碼中通過ArrayUtils類將數(shù)組轉換為CharSequence數(shù)組。
- 紅色
- 綠色
- 藍色
import androidx.annotation.NonNull;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import androidx.core.content.res.ResourcesCompat;
import org.apache.commons.lang3.ArrayUtils;
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ListcolorEntries = Arrays.asList(getResources().getStringArray(R.array.colors)); CharSequence[] colorCodes = ArrayUtils.toObject(colorEntries);
4、為ListPreference設置列表項圖標和提示文字樣式
如果希望為列表項添加圖標和自定義提示文字樣式,可以在資源文件中定義一個drawable和string數(shù)組,然后在代碼中通過TypedArray獲取這些資源ID,最后將這些資源ID設置到ListPreference的entryIcon和entryHint屬性上。
- @mipmap/red
- @mipmap/green
- @mipmap/blue
TypedArray typedArray = getResources().obtainTypedArray(R.array.color_codes);
int[] colorCodeIds = new int[typedArray.length()];
for (int i = 0; i < typedArray.length(); i++) {
colorCodeIds[i] = typedArray.getResourceId(i, R.mipmap.red); //默認圖標為紅色圓點圖標,如果沒有對應的資源ID則使用默認值,可以自行修改。!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&(((((((((((((((((((((((((((((((((((((((((((((((&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&%............................................................................................................%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}$}}}}}}$}}}}}}}}}}$}}}}}}$}}}}}}}}}$}}}}}}}}}}}}}$}}}}}}$}}}}}}}}}$}}}}}}$}}}}}}$}}}}}}}}}$}}}}}}$}}}}}}$}}}}}}}}}$}}}}}}}}}$}}}}}}}}}$}}}}|$
當前文章:android用戶界面框架采用什么模式
網(wǎng)站地址:http://www.fisionsoft.com.cn/article/dhghgse.html


咨詢
建站咨詢
