李堇 2023-04-13 16:19 采纳率: 51.4%
浏览 18
已结题

android自定义对话框如何修改布局外的半透明遮罩?

自定义CustomDialog继承Dialog后设置相应的布局后,想问问如何控制对话框外的半透明背景?
代码如下:

package com.mxjy.ssep.ui.subassembly;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.mxjy.ssep.R;
import com.mxjy.ssep.softKeyboard.KeyboardUtil;
import com.mxjy.ssep.ui.other.MyTransformation;
import com.mxjy.ssep.util.LogUtils;

/**
 * 退出图标对话框
 */
public class CustomDialog6 extends Dialog implements View.OnClickListener {

    private TextView mTvTitle,mTvMessage,mTvCancel,mTvConfirm;
    private EditText inputInfo;
    private String title,message,cancle,confirm;

    private IOnCancelListener cancelListener;
    private IOnConfirmListener confirmListener;

    private KeyboardUtil keyboardUtil;

    public String getTitle() {
        return title;
    }

    public EditText getInputInfo() {
        return inputInfo;
    }

    public void setInputInfo(EditText inputInfo) {
        this.inputInfo = inputInfo;
    }

    public CustomDialog6 setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public CustomDialog6 setMessage(String message) {
        this.message = message;
        return this;
    }

    public String getCancle() {
        return cancle;
    }

    public CustomDialog6 setCancle(String cancle, IOnCancelListener listener) {
        this.cancle = cancle;
        this.cancelListener = listener;
        return this;
    }

    public String getConfirm() {
        return confirm;
    }

    public CustomDialog6 setConfirm(String confirm, IOnConfirmListener listener) {
        this.confirm = confirm;
        this.confirmListener = listener;
        return this;
    }

    public CustomDialog6(@NonNull Context context) {
        super(context);

    }

    public CustomDialog6(@NonNull Context context,KeyboardUtil keyboardUtil) {
        super(context);
        this.keyboardUtil = keyboardUtil;

    }

    public CustomDialog6(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setContentView(R.layout.custom_dialog6);
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int)(size.x * 0.25);
        getWindow().setAttributes(p);

        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvMessage = (TextView) findViewById(R.id.tv_message);
        mTvCancel = (TextView) findViewById(R.id.tv_cancle);
        mTvConfirm = (TextView) findViewById(R.id.tv_confirm);
        inputInfo = (EditText) findViewById(R.id.inputInfo);
        inputInfo.setTransformationMethod(new MyTransformation());
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        keyboardUtil.setEditText(inputInfo);
        keyboardUtil.showKeyboard();
        inputInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //LogUtils.e("=4=","inputInfo:" + "OnClick触发");
                keyboardUtil.showKeyboard();
            }
        });

        if(!TextUtils.isEmpty(title)) {
            mTvTitle.setText(title);
        }
        if(!TextUtils.isEmpty(message)) {
            mTvMessage.setText(message);
        }
        if(!TextUtils.isEmpty(cancle)) {
            mTvCancel.setText(cancle);
        }
        if(!TextUtils.isEmpty(confirm)) {
            mTvConfirm.setText(confirm);
        }
        mTvCancel.setOnClickListener(this);
        mTvConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.tv_cancle:
                if(cancelListener != null) {
                    cancelListener.onCancel(this);
                }
                dismiss();
                break;
            case R.id.tv_confirm:
                if(confirmListener != null) {
                    confirmListener.onConfirm(this);
                }
                dismiss();
                break;
        }
    }

    public interface IOnCancelListener{
        void onCancel(CustomDialog6 dialog);
    }

    public interface IOnConfirmListener{
        void onConfirm(CustomDialog6 dialog);
    }

}

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/sharp11"
    >
    <LinearLayout
        android:id="@+id/mianV"
        android:layout_width="@dimen/dp_720"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        >

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/dp_12"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:gravity="center"
            android:paddingTop="@dimen/dp_20"

            android:text=""/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_10"
            android:textColor="@color/black"
            android:gravity="center"
            android:background="#ffffff"
            android:text=""/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:background="#ffffff"
            >
            <EditText
                android:id="@+id/inputInfo"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_20"
                android:layout_marginRight="@dimen/dp_20"
                android:layout_marginLeft="@dimen/dp_20"
                android:text=""
                android:textColor="@color/black"
                android:background="@drawable/sharp3"
                />
        </LinearLayout>


        <TextView
            android:id="@+id/</span>tv_message<span class="hljs-string">"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_0"
            android:background="#ffffff"
            android:gravity="center"
            android:paddingLeft="@dimen/dp_10"
            android:paddingRight="@dimen/dp_10"
            android:paddingTop="@dimen/dp_2"
            android:paddingBottom="@dimen/dp_2"
            android:text=""
            android:textColor="@color/black"
            android:textSize="@dimen/dp_8"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_40"
            android:orientation="horizontal"
            android:background="@drawable/sharp8">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/tv_cancle"
                    android:layout_width="@dimen/dp_35"
                    android:layout_height="@dimen/dp_20"
                    android:textSize="@dimen/dp_8"
                    android:textColor="#ffffff"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:background="@drawable/sharp9"
                    android:text="取消"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/</span>tv_confirm<span class="hljs-string">"
                    android:layout_width="@dimen/dp_35"
                    android:layout_height="@dimen/dp_20"
                    android:textSize="@dimen/dp_8"
                    android:textColor="#ffffff"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:background="@drawable/sharp10"
                    android:text="确认"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

效果如下:

img

  • 写回答

2条回答 默认 最新

  • 李堇 2023-04-14 14:09
    关注
    
    在style文件中添加如下代码:
    <style name="myDialogTheme" parent="@android:style/Theme.Dialog">
            <item name="android:windowNoTitle">true</item>
            <item name="android:backgroundDimEnabled">false</item>
     </style>
    其中这个 <item name="android:backgroundDimEnabled">false</item>设置是对话框外侧是否启用半透明遮罩开关。
    
    创建时使用CustomDialog6 customDialog6 = new CustomDialog6(getContext(),keyboardUtil,R.style.myDialogTheme);
    传递对应的主题编号。
    
    对应的构造函数中使用如下代码即可:
    public CustomDialog6(@NonNull Context context,KeyboardUtil keyboardUtil,int themeResId) {
            //super(context);
            super(context,themeResId);
            this.keyboardUtil = keyboardUtil;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月22日
  • 已采纳回答 4月14日
  • 创建了问题 4月13日

悬赏问题

  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么