Android 除非另有说明,否则不要关闭对话框窗口?
在对话框窗口中,用户输入搜索关键字。然后数据库查找它并返回对象列表。若列表大小为0,我想通知用户未找到任何内容,并保持对话框窗口打开,直到用户能够输入正确的关键字,结果超过0,或者直到用户决定退出对话框窗口 如果在数据库中找不到任何结果,我就不知道如何保持窗口打开。如能提供帮助,将不胜感激:Android 除非另有说明,否则不要关闭对话框窗口?,android,database,user-interface,android-alertdialog,Android,Database,User Interface,Android Alertdialog,在对话框窗口中,用户输入搜索关键字。然后数据库查找它并返回对象列表。若列表大小为0,我想通知用户未找到任何内容,并保持对话框窗口打开,直到用户能够输入正确的关键字,结果超过0,或者直到用户决定退出对话框窗口 如果在数据库中找不到任何结果,我就不知道如何保持窗口打开。如能提供帮助,将不胜感激: public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new Ale
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();
}
创建自定义对话框片段并覆盖按钮,我的旧代码中有一个工作示例:
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.
}
});
}
}
}
builder.setCancelablefalse@克里斯安迪,我试过了。必须将生成器声明为final,并在if子句内调用值为false的setCancelable方法,在if子句外调用值为true的setCancelable方法。似乎没有帮助。另外@ChrisHandy-想指出,根据谷歌的文档,setCancelable方法只能确定用户是否能够用后退按钮关闭对话框。啊,我明白了,所以我必须重写onStart以修改肯定按钮。非常感谢。我会尽快尝试。