1

我正在尝试使用要以编程方式填写的多选列表进行自定义首选项。我尝试创建自定义 DialogPreference 如下所示,并使用 AlertDialog 构建器创建我的对话框。问题是拼凑不同的教程,我正在实现/覆盖需要返回视图的 onCreateDialogView()。我不确定是否可以让 AlertDialog.Builder 返回视图。

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);
}
}

关于如何解决此问题的任何想法或建议是将构建器作为视图返回,或者以编程方式创建对话框的不同方式将不胜感激。

这是我试图结合的两个教程: 编写新 DialogPreference 类的简洁方式?Android:在 AlertDialog 内的多选 ListView 中选择项目

4

0 回答 0