在 Activity 中显示对话框或弹出浮层时,尽量使用 DialogFragment,而非
Dialog/AlertDialog,这样便于随Activity生命周期管理对话框/弹出浮层的生命周期。
官方推荐的。
下面我将通过几种方式实现对话框。
1、利用Alert Dialog复写onCreateDialog来实现对话框。
效果图
创建一个DialogAlertFragment继承DialogFragment。在这个类中实现对话框。
package com.hjqjl.testdemo.widget.dialogfragment;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
/**
* 使用方法示例,在activity中:
* DialogAlertFragment newFragment = DialogAlertFragment.newInstance2("我是title", "我是内容", "确定");
* newFragment.show(getSupportFragmentManager(), "dialog");
*/
//AlertDialog
public class DialogAlertFragment extends DialogFragment {
private Context mContext;
private DialogInterface.OnClickListener positiveOnClickListener;
private DialogInterface.OnClickListener negativeOnClickListener;
private DialogAlertFragment(DialogInterface.OnClickListener positiveOnClickListener, DialogInterface.OnClickListener negativeOnClickListener) {
this.positiveOnClickListener = positiveOnClickListener;
this.negativeOnClickListener = negativeOnClickListener;
}
public static DialogAlertFragment newInstance(String title, String message, String positiveStr, String negativeStr, DialogInterface.OnClickListener positiveOnClickListener, DialogInterface.OnClickListener negativeOnClickListener) {
DialogAlertFragment frag = new DialogAlertFragment(positiveOnClickListener, negativeOnClickListener);
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
args.putString("positiveStr", positiveStr);
args.putString("negativeStr", negativeStr);
frag.setArguments(args);
return frag;
}
//进行了简化,比如单纯通知类消息使用
public static DialogAlertFragment newInstance2(String title, String message, String positiveStr) {
return DialogAlertFragment.newInstance(title, message, positiveStr, "", null, null);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
mContext = context;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Bundle args = getArguments();
String title = "";
String message = "";
String positiveStr = "";
String negativeStr = "";
if (args != null) {
title = args.getString("title");
message = args.getString("message");
positiveStr = args.getString("positiveStr");
negativeStr = args.getString("negativeStr");
}
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);//getActivity()报警告有可能是null,所以用mContext
builder.setTitle(title)
.setMessage(message)
.setPositiveButton(positiveStr, positiveOnClickListener)
.setNegativeButton(negativeStr, negativeOnClickListener);
return builder.create();
}
}
在activity中使用显示出对话框
DialogAlertFragment alertDialogFragment = DialogAlertFragment.newInstance
("我是title", "我是内容--MyAlertDialogFragment", "确定", "取消"
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ToastUtils.showToast(mContext, "我是内容--MyAlertDialogFragment");
}
}
, null);
alertDialogFragment.show(getSupportFragmentManager(), "alertDialog");
2、自定义布局DialogFragment
通过覆写其 onCreateView、onViewCreated方法,创建自定义布局的对话框。
自定义布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_dialogfragment_title"
android:layout_width="match_parent"
android:layout_height="44dp"
android:gravity="center"
android:text="title"
android:textColor="#222"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_dialogfragment_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="我是自定义的DialogFragment" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#999" />
<TextView
android:id="@+id/tv_dialogfragment_confirm"
android:layout_width="match_parent"
android:layout_height="44dp"
android:gravity="center|end"
android:paddingEnd="55dp"
android:text="确定"
android:textColor="#4bc"
android:textSize="18sp" />
</LinearLayout>
自定义DialogFragment实现方法如下:
package com.hjqjl.testdemo.widget.dialogfragment;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import com.hjqjl.testdemo.databinding.DialogCustomBinding;
//自定义view的Dialog
/**
* DialogCustomFragment dialogCustomFragment = DialogCustomFragment.newInstance
* ("我是title", "我是内容--我是自定义的DialogFragment"
* , new DialogCustomFragment.OnClickMyListener() {
* @Override
* public void onMyClick(Dialog dialog) {
* ToastUtils.showToast(mContext, "我是内容--我是自定义的DialogFragment");
* dialog.dismiss();
* }
* });
* dialogCustomFragment.show(getSupportFragmentManager(), "customDialog");
* */
public class DialogCustomFragment extends DialogFragment {
private DialogCustomBinding binding;
private OnClickMyListener listener;
/**
* Listener
*/
public interface OnClickMyListener {
void onMyClick(Dialog dialog);
}
private DialogCustomFragment(OnClickMyListener listener) {
this.listener = listener;
}
public static DialogCustomFragment newInstance(String title, String message, OnClickMyListener listener) {
DialogCustomFragment fragment = new DialogCustomFragment(listener);
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("message", message);
fragment.setArguments(bundle);
return fragment;
}
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DialogCustomBinding.inflate(inflater, container, false);
View view = binding.getRoot();
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Bundle bundle = getArguments();
String title = "";
String message = "";
if (bundle != null) {
title = bundle.getString("title");
message = bundle.getString("message");
}
binding.tvDialogfragmentTitle.setText(title);
binding.tvDialogfragmentMessage.setText(message);
binding.tvDialogfragmentConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onMyClick(getDialog());
}
}
});
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
调用显示自定义CustomDialogFragment对话框:
DialogCustomFragment dialogCustomFragment = DialogCustomFragment.newInstance
("我是title", "我是内容--我是自定义的DialogFragment"
, new DialogCustomFragment.OnClickMyListener() {
@Override
public void onMyClick(Dialog dialog) {
ToastUtils.showToast(mContext, "我是内容--我是自定义的DialogFragment");
dialog.dismiss();
}
});
dialogCustomFragment.show(getSupportFragmentManager(), "customDialog");
3、DialogTopFragment、DialogBottomFragment和DialogLoadingFragment
分别是从顶部弹出的对话框和从底部弹出的对话框以及加载中对话框,里面的设置属性和点击事件可参考自定义布局DialogCustomFragment、DialogAlertFragment 大同小异。
顶部dialog:
底部dialog:
加载中dialog: