自定义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>
效果如下: