Android - 基于AndroidX的IconPicker首选项

问题描述 投票:0回答:1

多年来我一直在使用一个很好的 IconPickerPreference 就像这段代码中的

旧的 android.preference 库已弃用。大约 4 年以来,Android 提供了新的 androidx.preference 库。

执行搜索并用 androidx.preference 替换旧的 android.preference 并不是示例图标选择器首选项的有效解决方案。

你有 IconPickerPreference 的实现吗?每行由一个名称和一个图标组成。

我在网上找了好久都没有找到好的解决方案。我希望你能帮忙。

类似的问题没有提供好的答案。

我为 android.preference 找到的最接近我需要的解决方案如下。

问题是 @Override onPrepareDialogBuilder 不在 androidx.preference.ListPreference 类中。

android
1个回答
0
投票
经过大量实验,我想出了这个解决方案。

AndroidX 的 ListPreference.preference:

import androidx.appcompat.app.AlertDialog; import androidx.preference.ListPreference; public class CustomListPreference extends ListPreference { CustomListPreferenceAdapter customListPreferenceAdapter = null; Context mContext; private LayoutInflater mInflater; CharSequence[] entries; CharSequence[] entryValues; ArrayList<RadioButton> rButtonList; SharedPreferences prefs; SharedPreferences.Editor editor; private String mValue; private AlertDialog mDialog; public CustomListPreference(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; mInflater = LayoutInflater.from(context); rButtonList = new ArrayList<>(); } @Override protected void onClick() { entries = getEntries(); entryValues = getEntryValues(); mValue = getValue(); AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); customListPreferenceAdapter = new CustomListPreferenceAdapter(R.layout.custom_list_preference_row); builder.setAdapter(customListPreferenceAdapter, null); mDialog = builder.create(); mDialog.show(); } private class CustomListPreferenceAdapter extends BaseAdapter { int layoutIndex = 0; public CustomListPreferenceAdapter( int layoutIndex) { this.layoutIndex = layoutIndex; } public int getCount() { return entries.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { View row = convertView; CustomHolder holder = null; if(row == null) { row = mInflater.inflate(this.layoutIndex, parent, false); holder = new CustomHolder(row, position); row.setTag(holder); row.setClickable(true); } return row; } class CustomHolder { private TextView text = null; private RadioButton rButton = null; CustomHolder(View row, int position) { text = (TextView)row.findViewById(R.id.custom_list_view_row_text_view); text.setText(entries[position]); rButton = (RadioButton)row.findViewById(R.id.custom_list_view_row_radio_button); rButton.setId(position); } } } }
在 PreferenceScreen 中我得到了选择的结果。

© www.soinside.com 2019 - 2024. All rights reserved.