All*_*lan 9 android android-preferences
我在我的Android应用程序中使用listpreference并获取我的键值,一切都很好并且效果很好(现在你们帮助了我)但是 - 当我的listpreference菜单弹出时,它们只包含一个取消按钮.
假设用户在红色,蓝色和绿色之间进行选择.首次弹出列表首选项对话框时,对话框仅显示取消按钮.因此,只要用户选择了对话框,对话框就会消失.我希望这样当用户选择他们的设置时,他们会看到单选按钮突出显示然后他们继续并单击确定按钮...但我没有确定按钮而无法弄清楚原因.任何帮助都会很棒......
您可以克隆并重新实现ListPreference
以您希望的方式工作,从而创建自己的自定义Preference
类.
但是,ListPreference
设置为仅使用否定("取消")按钮.正如源代码所说:
/*
* The typical interaction for list-based dialogs is to have
* click-on-an-item dismiss the dialog instead of the user having to
* press 'Ok'.
*/
Run Code Online (Sandbox Code Playgroud)
我已经完成了之前的答案建议并基于Android源代码实现了我自己的ListPreference.下面是我添加OK按钮的实现.
myPreferenceList.java
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.preference.ListPreference;
import android.util.AttributeSet;
public class myPreferenceList extends ListPreference implements OnClickListener{
private int mClickedDialogEntryIndex;
public myPreferenceList(Context context, AttributeSet attrs) {
super(context, attrs);
}
public myPreferenceList(Context context) {
this(context, null);
}
private int getValueIndex() {
return findIndexOfValue(this.getValue() +"");
}
@Override
protected void onPrepareDialogBuilder(Builder builder) {
super.onPrepareDialogBuilder(builder);
mClickedDialogEntryIndex = getValueIndex();
builder.setSingleChoiceItems(this.getEntries(), mClickedDialogEntryIndex, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mClickedDialogEntryIndex = which;
}
});
System.out.println(getEntry() + " " + this.getEntries()[0]);
builder.setPositiveButton("OK", this);
}
public void onClick (DialogInterface dialog, int which)
{
this.setValue(this.getEntryValues()[mClickedDialogEntryIndex]+"");
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在preference.xml中使用该类,如下所示:
<com.yourApplicationName.myPreferenceList
android:key="yourKey"
android:entries="@array/yourEntries"
android:entryValues="@array/yourValues"
android:title="@string/yourTitle" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3578 次 |
最近记录: |