21

我想为我的应用程序创建模式对话框。

因此,当模式对话框打开时,其他活动被阻止。没有像按下后退按钮或按下主页按钮那样完成任何事件。

并在该对话框中放置两个选项按钮取消并确定。

谢谢...

4

4 回答 4

32

Android中有很多种Dialogs。请看一下对话框。我猜你正在寻找的是类似的东西AlertDialog。这是如何在BackPress按钮上实现的示例。

@Override
public void onBackPressed() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Do you want to logout?");
    // alert.setMessage("Message");

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //Your action here
        }
    });

    alert.setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        });

    alert.show();

}
于 2013-08-22T04:55:58.217 回答
10

使用可以使用 setCancellable(false); setCanceledOnTouchOutside(false); 对于对话框本身,这应该阻止该对话框通过 BACK 关闭并在对话框外部点击。

您不能覆盖 HOME 按钮。

于 2013-08-22T05:00:48.823 回答
9

尝试这个::

您需要创建要在弹出窗口中显示的布局。您可以创建布局 XML 并像这样使用它:

LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
            View layout = layoutInflater.inflate(R.layout.new_popup_layout, null);  
            final PopupWindow popupWindow = new PopupWindow(
                    layout, 
                       LayoutParams.WRAP_CONTENT,  
                             LayoutParams.WRAP_CONTENT);

您还可以像这样提供按钮的单击事件:

ImageButton btnChoose = (ImageButton) layout.findViewById(R.id.btnChoose);
            btnChoose.setOnClickListener(new OnClickListener()  {

                @Override
                public void onClick(View v) {
}
});

并像这样显示这个弹出窗口:在这里你想在按钮单击时显示这个,然后按钮视图就会出现。

 popupWindow.showAtLocation(anyview,Gravity.CENTER, 0, 0);
于 2013-08-22T04:59:15.527 回答
7

尝试如下:

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setMessage("Are you sure you want to exit?")
  .setCancelable(false)
   .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
        MyActivity.this.finish();
   }
 })
 .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
   }
});
AlertDialog alert = builder.create();

对于 Home Key 事件:

不,无法在 android 中获取 Home 键事件。从主页键码的文档:http: //developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME

公共静态最终 int KEYCODE_HOME

键码常量:Home键。该密钥由框架处理,从不传递给应用程序

于 2013-08-22T05:01:28.897 回答