Android 安卓自定义Dialog工具类封装与使用

Android 安卓Dialog工具类封装与使用
安卓自定义Dialog,分别用了Kotlin和Java两种语言列出!布局可以自定义!

效果图
布局是自定义的

配置
style.xml中配置

<style name=”BoxDialog” parent=”@android:style/Theme.Holo.Dialog”>
<!– 是否有边框 –>
<item name=”android:windowFrame”>@null</item>
<!–是否在悬浮Activity之上 –>
<item name=”android:windowIsFloating”>true</item>
<!– 标题 –>
<item name=”android:windowNoTitle”>true</item>
<!–阴影 –>
<item name=”android:windowIsTranslucent”>true</item>
<!–背景透明–>
<item name=”android:windowBackground”>@android:color/transparent</item>
<!–可加入动画–>
</style>

Java
/**
* @author ThirdGoddess
* @email ofmyhub@gmail.com
* @Github https://github.com/ThirdGoddess
* @date :2019-12-29 01:24
*/
public class BoxDialog extends Dialog {

//Dialog View
private View view;

//Dialog弹出位置
private LocationView locationView = LocationView.CENTER;

/**
* @param context 上下文
* @param view Dialog View
*/
public BoxDialog(Context context, View view) {
super(context, R.style.BoxDialog);
this.view = view;
}

/**
* @param context 上下文
* @param view Dialog View
* @param locationView Dialog弹出位置
*/
public BoxDialog(Context context, View view, LocationView locationView) {
super(context, R.style.BoxDialog);
this.view = view;
this.locationView = locationView;
}

@SuppressLint(“RtlHardcoded”)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (null != view) {
setContentView(view);
setCancelable(isCancelable);//点击外部是否可以关闭Dialog
setCanceledOnTouchOutside(isCanceledOnTouchOutside);//返回键是否可以关闭Dialog
Window window = this.getWindow();
assert window != null;
switch (locationView) {
case TOP:
window.setGravity(Gravity.TOP);
break;
case BOTTOM:
window.setGravity(Gravity.BOTTOM);
break;
case CENTER:
window.setGravity(Gravity.CENTER);
break;
}
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(params);
}
}

public enum LocationView {
CENTER, TOP, BOTTOM
}
}

Kotlin
/**
* @author ThirdGoddess
* @email ofmyhub@gmail.com
* @Github https://github.com/ThirdGoddess
* @date :2019-12-29 01:24
*/
class BoxDialog : Dialog {

//Dialog View
private var view: View?

//Dialog弹出位置
private var locationView = LocationView.CENTER

/**
* @param context 上下文
* @param view Dialog View
*/
constructor(context: Context?, view: View?) : super(context!!, R.style.MyDialog) {
this.view = view
}

/**
* @param context 上下文
* @param view Dialog View
* @param locationView Dialog弹出位置
*/
constructor(context: Context?, view: View?, locationView: LocationView) : super(context!!, R.style.MyDialog) {
this.view = view
this.locationView = locationView
}

@SuppressLint(“RtlHardcoded”)
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
if (null != view) {
setContentView(view!!)
setCancelable(isCancelable) //点击外部是否可以关闭Dialog
setCanceledOnTouchOutside(isCanceledOnTouchOutside) //返回键是否可以关闭Dialog
val window = this.window!!
when (locationView) {
LocationView.TOP -> window.setGravity(Gravity.TOP)
LocationView.BOTTOM -> window.setGravity(Gravity.BOTTOM)
LocationView.CENTER -> window.setGravity(Gravity.CENTER)
}
val params = window.attributes
params.width = WindowManager.LayoutParams.MATCH_PARENT
params.height = WindowManager.LayoutParams.WRAP_CONTENT
window.attributes = params
}
}

enum class LocationView {
CENTER, TOP, BOTTOM
}
}

使用方法
Java 和 Kotlin 的方法

Java
public class MainActivity extends AppCompatActivity {

private BoxDialog boxDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}

private void initView() {
Button dialogButton = findViewById(R.id.dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_view2, null, false);
//在这里可以给布局中的按钮加事件,boxDialog.dismiss()可以关闭dialog

boxDialog = new BoxDialog(MainActivity.this, inflate, BoxDialog.LocationView.CENTER);
boxDialog.setCancelable(false);//是否可以点击DialogView外关闭Dialog
boxDialog.setCanceledOnTouchOutside(false);//是否可以按返回按钮关闭Dialog
boxDialog.show();
}
});
}

}

Kotlin
class MainActivity : AppCompatActivity() {
private var boxDialog: BoxDialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initView()
}

private fun initView() {
val dialogButton = findViewById<Button>(R.id.dialog)
dialogButton.setOnClickListener {
val inflate = LayoutInflater.from(this).inflate(R.layout.dialog_view2, null, false)
//可以给布局中的按钮加事件,boxDialog.dismiss();关闭dialog

boxDialog = BoxDialog(this, inflate, BoxDialog.LocationView.CENTER)
boxDialog!!.setCancelable(false) //是否可以点击DialogView外关闭Dialog
boxDialog!!.setCanceledOnTouchOutside(false) //是否可以按返回按钮关闭Dialog
boxDialog!!.show()
}
}
}

安卓自定义Dialog的实现

一、Dialog布局文件

<?xml version=”1.0″ encoding=”utf-8″?>
<FrameLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:clickable=”true”
android:orientation=”vertical”
android:padding=”20.0dip” >

<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:background=”#ff7200″
android:orientation=”vertical” >

<TextView
android:id=”@+id/title”
android:textColor=”#ffffff”
android:textSize=”20sp”
android:layout_width=”fill_parent”
android:layout_height=”50dp”
android:gravity=”center”
android:text=”自定义弹窗”
android:visibility=”visible” />

<LinearLayout
android:id=”@+id/content”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:gravity=”center” >

<TextView
android:background=”#ffffff”
android:id=”@+id/message”
android:textColor=”#b8b8b8″
android:textSize=”16sp”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:gravity=”left|center”
android:lineSpacingMultiplier=”1.5″
android:paddingBottom=”15.0dip”
android:paddingLeft=”20.0dip”
android:paddingRight=”20.0dip”
android:paddingTop=”15.0dip” />
</LinearLayout>

<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”50dp”
android:layout_gravity=”bottom”
android:background=”#ffffff”
android:gravity=”center”
android:orientation=”horizontal” >

<TextView
android:layout_weight=”1″
android:id=”@+id/positiveTextView”
android:textColor=”#b8b8b8″
android:textSize=”16sp”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:text=”ok”
android:clickable=”true”/>

<TextView
android:layout_weight=”1″
android:id=”@+id/negativeTextView”
android:textColor=”#ff9138″
android:textSize=”16sp”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:text=”cancel”
android:clickable=”true”/>
</LinearLayout>
</LinearLayout>

</FrameLayout>

二、Style

<style name=”Dialog” parent=”android:style/Theme.Dialog”>
<item name=”android:background”>#00000000</item>
<item name=”android:windowBackground”>@android:color/transparent</item>
<item name=”android:windowNoTitle”>true</item>
<item name=”android:windowIsFloating”>true</item>
</style>

三、MainActivity代码

CustomDialog.Builder builder = new CustomDialog.Builder(MainActivity.this);
builder.setMessage(“这个就是自定义的提示框”);
builder.setTitle(“提示”);
builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//设置你的操作事项
Toast.makeText(MainActivity.this,”queding”,Toast.LENGTH_SHORT).show();
}
});

builder.setNegativeButton(“取消”,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,”queding”,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});

builder.create().show();

四、自定义DialogClass

package com.cavytech.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cavytech.wear2.R;

/**
* Created by LiBin on 2016/6/16.
*/
public class CustomDialog extends Dialog {

public CustomDialog(Context context) {
super(context);
}

public CustomDialog(Context context, int theme) {
super(context, theme);
}

public static class Builder {
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private DialogInterface.OnClickListener positiveButtonClickListener;
private DialogInterface.OnClickListener negativeButtonClickListener;

public Builder(Context context) {
this.context = context;
}

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

/**
* Set the Dialog message from resource
*
* @param
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}

/**
* Set the Dialog title from resource
*
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}

/**
* Set the Dialog title from String
*
* @param title
* @return
*/

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

public Builder setContentView(View v) {
this.contentView = v;
return this;
}

/**
* Set the positive button resource and it’s listener
*
* @param positiveButtonText
* @return
*/
public Builder setPositiveButton(int positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}

public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}

public Builder setNegativeButton(int negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = (String) context
.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}

public Builder setNegativeButton(String negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}

public CustomDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final CustomDialog dialog = new CustomDialog(context, R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
dialog.addContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
// set the dialog title
((TextView) layout.findViewById(R.id.title)).setText(title);
// set the confirm button
if (positiveButtonText != null) {
((TextView) layout.findViewById(R.id.positiveTextView))
.setText(positiveButtonText);
if (positiveButtonClickListener != null) {
((TextView) layout.findViewById(R.id.positiveTextView))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.positiveTextView).setVisibility(
View.GONE);
}
// set the cancel button
if (negativeButtonText != null) {
((TextView) layout.findViewById(R.id.negativeTextView))
.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
((TextView) layout.findViewById(R.id.negativeTextView))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.negativeTextView).setVisibility(
View.GONE);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(R.id.message)).setText(message);
} else if (contentView != null) {
// if no message set
// add the contentView to the dialog body
((LinearLayout) layout.findViewById(R.id.content))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.content))
.addView(contentView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}