Tôi đã làm câu trả lời trước đó được đề xuất và triển khai ListPreference của riêng tôi dựa trên mã nguồn Android. Dưới đây là thực hiện của tôi mà thêm nút 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]+"");
}
}
Sau đó, bạn có thể sử dụng các lớp trong preference.xml của bạn như sau:
<com.yourApplicationName.myPreferenceList
android:key="yourKey"
android:entries="@array/yourEntries"
android:entryValues="@array/yourValues"
android:title="@string/yourTitle" />
Để cụ thể hơn: thực hiện DialogInterface.OnClickListener – southerton