我为我的应用程序实现了一个自定义对话框.我希望实现当用户在对话框外单击时,对话框将被取消.我该怎么做?
android dialog android-emulator android-layout android-dialog
我正在开发一个应用程序,当按下按钮时,它会打开一个带有确定和取消按钮的对话框.
它工作正常.
当用户按下后退按钮时,我按如下方式处理
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
}
return super.onKeyDown(keyCode, event);
}
Run Code Online (Sandbox Code Playgroud)
但是没有调用上面的方法.我怎么处理这个?
这个DialogFragment实现导致了
IllegalStateException("你不能设置Dialog的OnCancelListener或OnDismissListener")
.为什么?解?
public class OkCThreadDialog1 extends DialogFragment{
DialogInterface.OnCancelListener onCancelListener;
public OkCThreadDialog1(){
}
public static OkCThreadDialog1 newInstance(String title, String message) {
OkCThreadDialog1 frag = new OkCThreadDialog1();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
frag.setArguments(args);
return frag;
}
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder .setTitle(getArguments().getString("title"))
.setMessage(getArguments().getString("message"))
.setOnCancelListener(onCancelListener)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
getDialog().cancel();
}});
return builder.create();
} …
Run Code Online (Sandbox Code Playgroud) 我的活动打开一个对话框.当它关闭时,我需要ReloadTable()
执行该功能.所以我试图使用,setOnDismissListener
但它没有被触发.有人可以帮助我做错了吗?
谢谢!
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.transaction, null);
builder = new AlertDialog.Builder(new ContextThemeWrapper(TransactionsList.this , R.style.dialogwithoutdim));
builder.setView(layout);
alertDialog = builder.create();
alertDialog.setOnDismissListener(new OnDismissListener() {
public void onDismiss(final DialogInterface dialog) {
ReloadTable();
}
});
builder.show();
Run Code Online (Sandbox Code Playgroud)