李堇 2023-04-13 16:19 采纳率: 48.7%
浏览 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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化