【问题标题】:Close custom popup when it pressed outsided of the popup [duplicate]在弹出窗口外按下时关闭自定义弹出窗口[重复]
【发布时间】:2018-04-03 00:53:15
【问题描述】:

我正在使用 android 中的对话框创建自定义弹出窗口。使用自定义弹出窗口时,我使用取消按钮来关闭它。但现在我想通过在弹出窗口之外触摸来关闭它。怎么做?

我到目前为止所尝试的

private void showDetails() {
    final Dialog dialog = new Dialog(mContext, android.R.style.Theme_Translucent);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.popup_base_menu);
    dialog.setCanceledOnTouchOutside(true);

    text_cancel = (TextView) dialog.findViewById(R.id.text_cancel);
    txt_create_group_chat = (TextView) dialog.findViewById(R.id.txt_create_group_chat);
    txt_create_chat = (TextView) dialog.findViewById(R.id.txt_create_chat);
    txt_create_chatroom = dialog.findViewById(R.id.txt_create_chatroom);

    text_cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();

        }
    });

    txt_create_group_chat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(mContext, CreateGroupChat.class);
            startActivity(in);
            dialog.cancel();
        }
    });
    txt_create_chat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent in = new Intent(mContext, SearchActivity.class);
            startActivity(in);
            dialog.cancel();
        }
    });
    txt_create_chatroom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent in = new Intent(mContext, CreateChatroomActivity.class);
            startActivity(in);
            dialog.cancel();
        }
    });

    dialog.show();

}

【问题讨论】:

  • 你试过dialog.setCancelable(true); 吗?
  • 是的。没用

标签: android dialog android-popupwindow


【解决方案1】:

在显示对话框之前要进行三项更改:

 dialog.setCanceledOnTouchOutside(true);
 setCancelable(boolean)

最后可能有帮助的是转到布局 popup_base_menu 并将根视图 heightwidth 更改为:

 wrap_content

它应该代替match_parent。 所有这些更改都应该在dialog.show();之前调用

【讨论】:

    【解决方案2】:

    使用

    setCanceledOnTouchOutside(true)
    

    联系 Android 开发者documentation

    并使用AlertDialog.Builder 创建您的对话框,如here 所示。此类提供与您使用的 Dialog 相同的构造函数参数。

    另外,这个问题已经有一个answer

    【讨论】:

    • 已经是用户。但也没有用。
    • 抱歉,我的代码无法使用。
    • 也许我可以在chat为您提供更多帮助。
    猜你喜欢
    • 2012-12-21
    • 2023-02-09
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多