我正在尝试使用我想要以编程方式填写的多选列表来创建自定义首选项。我尝试创建一个自定义DialogPreference,如下所示,并使用AlertDialog构建器来创建我的对话框。问题是拼凑不同的教程,我正在实现/覆盖onCreateDialogView(),它需要返回一个View。我不确定是否有可能让AlertDialog.Builder返回一个View。
public class notifyPreferances extends DialogPreference {
//Tag used for logcat
String TAG = "notifyPreferances";
//Store values to diplay in mutliselect list
CharSequence[] selections;
//Constructor
notifyPreferances(Context context, AttributeSet attrs, CharSequence[] options) {
super(context, attrs);
setPersistent(false);
selections = options;
}
@Override
protected View onCreateDialogView() {
//Initialize Builer
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
//Set Dialog Title
builder.setTitle("Lights/Groups")
//Set the items for the list and the onclick actions
builder.setItems(selections, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Log.i(TAG, "Selected Option: " + selections[item]);
}
});
return builder.create();
return super.onCreateDialogView();
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
}
}
关于如何解决此问题的任何想法或建议要么将构建器作为视图返回,要么以不同的方式以编程方式创建对话框将非常感激。
以下是我要尝试合并的两个教程: Concise way of writing new DialogPreference classes?和 Android: Select items in a multi-select ListView inside AlertDialog