我有一个带有输入字段和两个按钮(还原,保存)的警报对话框.
当我单击手机上的“后退”按钮时,我要d出另一个确认对话框,询问:“确定要完成吗?”.所以一切看起来像这样:
public voID showNewItemDialog(final int...position) { LayoutInflater li = LayoutInflater.from(HostActivity.this); VIEw promptsVIEw = li.inflate(R.layout.item_dialog, null); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( HostActivity.this); alertDialogBuilder.setVIEw(promptsVIEw); userinput = (EditText) promptsVIEw.findVIEwByID(R.ID.editTextDialogUserinput); if(position.length>0){ userinput.setText(ListFragment.getmItems().get(position[0]).getTitle()); userinput.setSelection(userinput.length()); userinput.requestFocus(); } alertDialogBuilder .setCancelable(false) .setPositivebutton("OK", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int ID) { String Title = userinput.getText().toString(); if(ListFragment.getItemClickType() == Utility.ItemClick.SHORT){ ListFragment.editRowItem(Title, position[0]); }else if(ListFragment.getItemClickType() == Utility.ItemClick.LONG){ }else if(ListFragment.getItemClickType() == Utility.ItemClick.ADD_button){ ListFragment.addRowItem(Title); } } }) .setNegativebutton("Revert", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int ID) { dialog.cancel(); } }); newItemalertDialog = alertDialogBuilder.create(); newItemalertDialog.setonKeyListener(new Dialog.OnKeyListener() { @OverrIDe public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { new AlertDialog.Builder(HostActivity.this) .setIcon(androID.R.drawable.ic_dialog_alert) .setTitle("Add Item") .setMessage("Are you sure you want to finish?") .setPositivebutton("Yes", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { dialog.dismiss(); newItemalertDialog.dismiss(); } }).setNegativebutton("No", null).show(); } return false; } }); newItemalertDialog.show();}
一切正常,但是仅当我在任意按钮上轻按两次(否,是)后,第二个确认对话框才会关闭.
我似乎找不到原因.
谢谢.
解决方法:
OnKey方法被调用了两次:第一次是按下按键,第二次是按下按键,因此您必须过滤:
所以更改如下代码
newItemalertDialog.setonKeyListener(new Dialog.OnKeyListener() { @OverrIDe public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) { if (event.getAction() != KeyEvent.ACTION_DOWN) return true; if (keyCode == KeyEvent.KEYCODE_BACK) { new AlertDialog.Builder(MemberShipActivity.this) .setIcon(androID.R.drawable.ic_dialog_alert) .setTitle("Add Item") .setMessage("Are you sure you want to finish?") .setPositivebutton("Yes", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { dialog.cancel(); newItemalertDialog.dismiss(); } }).setNegativebutton("No", null).show(); Log.e("Key","back"); } return false; } });
总结 以上是内存溢出为你收集整理的Android:AlertDialog仅在第二次单击任何按钮后关闭全部内容,希望文章能够帮你解决Android:AlertDialog仅在第二次单击任何按钮后关闭所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)