点击dialog空白处禁止关闭dialog

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_38373150/article/details/80061916
//点击空白处不关闭dialog
dialog.setCancelable(false);

dialog 完整代码

package com.developer.jim.wizard.views;

import android.app.Activity;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.developer.jim.wizard.R;

public class HuafuDialog extends DialogFragment {

    private static Activity mactivity;
    private static HuafuDialog dialog;

    public static void showHuafuDialog(Activity activity, String tag) {
        mactivity = activity;
        dialog = new HuafuDialog();
        dialog.show(activity.getFragmentManager(), tag);
        //点击空白处不关闭dialog
        dialog.setCancelable(false);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.3f;//设置透明度,比如0.3f
        window.setAttributes(windowParams);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = LayoutInflater.from(mactivity).inflate(R.layout.popup_huafu, container, false);
        Button cancel = view.findViewById(R.id.cancel);
        Button ensure = view.findViewById(R.id.ensure);
        ensure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        return view;
    }
}

展开阅读全文

点击imageview关闭dialog就报错?

08-17

java代码:rn[code=Java]rnpackage com.xiuman.mydialog;rnrnimport android.app.Dialog;rnimport android.content.Context;rnimport android.os.Bundle;rnimport android.view.View;rnimport android.widget.ImageView;rnrnpublic class MyDialog extends Dialog rnrn Context context;rn public MyDialog(Context context) rn super(context);rn // TODO Auto-generated constructor stubrn this.context = context;rn rn public MyDialog(Context context, int theme)rn super(context, theme);rn this.context = context;rn rn @Overridern protected void onCreate(Bundle savedInstanceState) rn // TODO Auto-generated method stubrn super.onCreate(savedInstanceState);rn this.setContentView(R.layout.dialog);rn ImageView imageview = (ImageView)findViewById(R.id.imageviewClose);rn imageview.setOnClickListener(new View.OnClickListener() rn rn public void onClick(View v) rn dismiss();rn rn );rn rn rn public void dismiss()rn this.cancel();rn rnrnrn[/code]rnrnlog:rn[code=Java]rn08-17 15:32:42.080: E/AndroidRuntime(1326): FATAL EXCEPTION: mainrn08-17 15:32:42.080: E/AndroidRuntime(1326): java.lang.StackOverflowErrorrn08-17 15:32:42.080: E/AndroidRuntime(1326): at com.xiuman.mydialog.MyDialog.dismiss(MyDialog.java:37)rn08-17 15:32:42.080: E/AndroidRuntime(1326): at android.app.Dialog.cancel(Dialog.java:951)rn08-17 15:32:42.080: E/AndroidRuntime(1326): at com.xiuman.mydialog.MyDialog.dismiss(MyDialog.java:37)rn08-17 15:32:42.080: E/AndroidRuntime(1326): at android.app.Dialog.cancel(Dialog.java:951)rn08-17 15:32:42.080: E/AndroidRuntime(1326): at com.xiuman.mydialog.MyDialog.dismiss(MyDialog.java:37)rn08-17 15:32:42.080: E/AndroidRuntime(1326): at android.app.Dialog.cancel(Dialog.java:951)rn08-17 15:32:42.080: E/AndroidRuntime(1326): at com.xiuman.mydialog.MyDialog.dismiss(MyDialog.java:37)rn08-17 15:32:42.080: E/AndroidRuntime(1326): at android.app.Dialog.cancel(Dialog.java:951)rnrn[/code] 论坛

没有更多推荐了,返回首页