除非另有说明,否则不要关闭对话窗口?

时间:2015-01-22 16:00:00

标签: android database user-interface android-alertdialog

在对话窗口中,用户输入搜索关键字。然后数据库查找它并返回对象列表。如果列表大小为0,我想通知用户没有找到任何内容并保持对话框窗口打开,直到用户能够输入超过0结果的正确关键字或用户决定退出对话窗口。

如果在DB中找不到任何结果,我无法弄清楚如何保持窗口打开。帮助将不胜感激:

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final EditText input = new EditText(getActivity());
    input.setHint("Enter keyword");

    builder.setTitle("Search").setView(input)
            .setPositiveButton("DONE", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    newExams = database.searchList(input.getText().toString());
                    if (newExams.size() == 0) {
                        Toast.makeText(getActivity(), "Nothing!", Toast.LENGTH_LONG).show();
                        return;
// here, I would like to make sure that window will be opened for second attempt of inputting a keyword
                    }

                    ExamAdapter newAdapter = new ExamAdapter(getActivity(), newExams);
                    listViewExams.setAdapter(newAdapter);
                }
            })
            .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });
    return builder.create();
}

1 个答案:

答案 0 :(得分:0)

创建自定义对话框片段并覆盖您的按钮,我的旧代码有一个工作示例:

public class MyAlertDialogFragment extends DialogFragment {


public static MyAlertDialogFragment newInstance(int title) {
    MyAlertDialogFragment frag = new MyAlertDialogFragment();
    Bundle args = new Bundle();
    args.putInt("title", title);
    frag.setArguments(args);

    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int title = getArguments().getInt("title");
    final View view = getActivity().getLayoutInflater().inflate(
            R.layout.dialoglayout, null);

    return new AlertDialog.Builder(getActivity())
            // .setIcon(R.drawable.icon)
            .setTitle(title)
            .setView(view)
            .setPositiveButton(getActivity().getResources().getString(R.string.dialog_save),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            //do something
                            dismiss();

                        }
                    })
            .setNegativeButton(getActivity().getResources().getString(R.string.dialog_cancel),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            //do something
                        }
                    })
            .setNeutralButton(getActivity().getResources().getString(R.string.dialog_clear),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                        }
                    }).create();
    }

    @Override
    public void onStart() {
        super.onStart(); // super.onStart() is where dialog.show() is actually
                     // called on the underlying dialog, so we have to do
                     // it after this point
        AlertDialog d = (AlertDialog) getDialog();
        if (d != null) {
            Button neutralButton = (Button) d.getButton(Dialog.BUTTON_NEUTRAL);
            // neutralButton.setBackgroundResource(R.drawable.xloginbutton);
            // neutralButton.setTextColor(Color.parseColor("#FFFFFF"));
            neutralButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Boolean wantToCloseDialog = false;
                // Do stuff, possibly set wantToCloseDialog to true then...
                if (wantToCloseDialog) {
                    dismiss();
                }

                // else dialog stays open. Make sure you have an obvious way
                // to close the dialog especially if you set cancellable to
                // false.
                }
            });

        }
    }
}