鸿洋博客介绍:DialogFragment的基本使用
好处:
1:使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其声明周期,它和Fragment有着基本一致的生命周期
2.DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用,类似Fragment(可以在大屏幕和小屏幕显示出不同的效果)
使用方法:
Dialoger.build(getActivity())
.setContentText("你好防盗链发交电费")
.setContentTitle("标题")
.setNegativeBtn("重新认证", new Dialoger.OnNegativeListener() {
@Override public void onNegative(Dialog dialog) {
dialog.dismiss();
....
....
} })
.setPositiveBtn("确认", new Dialoger.OnPositiveListener() {
@Override public void onPositive(Dialog dialog) {
dialog.dismiss();
....//对应的操作
....
}
})
.dialoger.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
view.getViewActivity().finish();
}
}). show();
封装:既然DialogFragment比普通的dialog好这么多,岂有不封装哪来用的道理
使用DialogFragment至少需要实现onCreateView或者onCreateDIalog方法。onCreateView即使用定义的xml布局文件展示Dialog。onCreateDialog即利用AlertDialog或者Dialog创建出Dialog。下面的封装采用onCreateView的方式来封装
1.构建dialog 设置基本参数
public static Dialoger build(FragmentActivity activity, String transactionTag, Bundle args) {
Dialoger dialoger = new Dialoger();
dialoger.setActivity(activity);//设置传入的activity对象
dialoger.setTransactionTag(transactionTag);//设置的tag标志(和Fragment一样,便于利用tag获取当前的Activity对象,来传递数据)
dialoger.setArguments(args);//设置的bundle参数
dialoger.setCancelable(false);//初始化设置为不可点击
dialoger.setTheme(0);//设置没有主题
dialoger.setContentView(R.layout.dialog_default);//设置默认的dialog的View
return dialoger;
}
public static Dialoger build(FragmentActivity activity) {
return build(activity, Dialoger.class.getSimpleName(), null);
}
2.外界传入对应的参数 (标题,context 内容 定义的View)
private void setActivity(FragmentActivity mActivity) {
this.mActivity = mActivity;
}
private void setTransactionTag(String transactionTag) {
this.mTransactionTag = transactionTag;
}
public Dialoger setContentView(int layoutResID) {
this.mContentView = LayoutInflater.from(mActivity).inflate(layoutResID, null);
return this;
}
public Dialoger setContentView(View view) {
this.mContentView = view;
return this;
}
public Dialoger setTheme(int theme) {
setStyle(DialogFragment.STYLE_NO_TITLE, theme);
return this;
}
public Dialoger setContentTitle(String title) {
mContentTitle = title;
return this;
}
public Dialoger setContentText(String text) {
mContentText = text;
return this;
}
public Dialoger setContentTitle(String title, int titleColor) {
mContentTitle = title;
mContentTitleColor = titleColor;
return this;
}
public Dialoger setContentText(String text, int textColor) {
mContentText = text;
mContentTextColor = textColor;
return this;
}
3.对传入进来的参数进行配置
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//设置透明背景
return mContentView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
if (mCancelListener != null) {//只有当设置了取消监听的时候,才可以设置取消
setCancelable(true);
}
//设置标题
TextView mTitleView = (TextView) view.findViewById(R.id.dialog_title_tv);
if (!TextUtils.isEmpty(mContentTitle)) {
mTitleView.setText(mContentTitle);
if (mContentTitleColor != 0) {
mTitleView.setTextColor(mContentTitleColor);
}
} else {
mTitleView.setVisibility(View.GONE);
}
//设置内容
TextView mTextView = (TextView) view.findViewById(R.id.dialog_text_tv);
if (!TextUtils.isEmpty(mContentText)) {
mTextView.setText(mContentText);
if (mContentTextColor != 0) {
mTextView.setTextColor(mContentTextColor);
}
} else {
mTextView.setVisibility(View.GONE);
}
//设置 确定 和 取消 按钮
View buttonPanel = view.findViewById(R.id.buttons_layout);
buttonPanel.setVisibility(View.GONE);
boolean negativeEnable = false;
boolean positiveEnable = false;
//如果有确定按钮 设置 ----->可点击 内容 监听
Button negativeButton = (Button) view.findViewById(R.id.btn_negative);
if (TextUtils.isEmpty(mNegativeText)) {
negativeButton.setVisibility(View.GONE);
} else {
negativeEnable = true;
buttonPanel.setVisibility(View.VISIBLE);
negativeButton.setVisibility(View.VISIBLE);
negativeButton.setText(mNegativeText);
negativeButton.setOnClickListener(this);
if (mNegativeTextColor != 0) {
negativeButton.setTextColor(mNegativeTextColor);
}
}
//如果有取消按钮 设置 ----->可点击 内容 监听
Button positiveButton = (Button) view.findViewById(R.id.btn_positive);
if (TextUtils.isEmpty(mPositiveText)) {
positiveButton.setVisibility(View.GONE);
} else {
positiveEnable = true;
buttonPanel.setVisibility(View.VISIBLE);
positiveButton.setVisibility(View.VISIBLE);
positiveButton.setText(mPositiveText);
positiveButton.setOnClickListener(this);
if (mPositiveTextColor != 0) {
positiveButton.setTextColor(mPositiveTextColor);
}
}
View contentDivider = view.findViewById(R.id.horizontal_divider);
View buttonDivider = view.findViewById(R.id.vertical_divider);
//设置布局的分割线 显示和隐藏
if (positiveEnable) {
if (negativeEnable) {
contentDivider.setVisibility(View.VISIBLE);
buttonDivider.setVisibility(View.VISIBLE);
} else {
contentDivider.setVisibility(View.GONE);
buttonDivider.setVisibility(View.GONE);
}
buttonPanel.setVisibility(View.VISIBLE);
} else {
if (negativeEnable) {
contentDivider.setVisibility(View.VISIBLE);
buttonPanel.setVisibility(View.VISIBLE);
buttonDivider.setVisibility(View.GONE);
} else {
contentDivider.setVisibility(View.GONE);
}
}
}
4.设置确定 取消按钮 和 取消监听
/**
* 确认
*
* @param text
* @param listener
* @return
*/
public Dialoger setPositiveBtn(String text, OnPositiveListener listener) {
mPositiveText = text;
mPositiveListener = listener;
return this;
}
public Dialoger setPositiveColor(int textColor) {
mPositiveTextColor = textColor;
return this;
}
/**
* 取消按钮(不是点击dialog其他地方的取消)
*
* @param text
* @param listener
* @return
*/
public Dialoger setNegativeBtn(String text, OnNegativeListener listener) {
mNegativeText = text;
mNegativeListener = listener;
return this;
}
public Dialoger setNegativeColor(int textColor) {
mNegativeTextColor = textColor;
return this;
}
//取消监听
public void setOnCancelListener(@Nullable DialogInterface.OnCancelListener listener) {
if (listener != null) {
mCancelListener = listener;
}
}
//调用dialog的取消
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
if (mCancelListener != null) {
mCancelListener.onCancel(dialog);
}
}
5.传递 确认 取消 的点击事件来回调 第4条的监听
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_negative) {
if (mNegativeListener != null) mNegativeListener.onNegative(getDialog());
} else if (v.getId() == R.id.btn_positive) {
if (mPositiveListener != null) mPositiveListener.onPositive(getDialog());
}
}
6.xml布局
style="@style/DT_DIALOG_THEME"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="30dip"
android:background="@drawable/dialog_bg_color"
android:minWidth="270dp"
android:orientation="vertical">
android:id="@+id/dialog_title_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="提示"
android:textColor="#333333"
android:textSize="18sp"/>
android:id="@+id/dialog_text_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="30dp"
android:gravity="center"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="提示"
android:textColor="#686871"
android:textSize="16sp"/>
android:id="@+id/horizontal_divider"
style="@style/horizontal_divider"/>
android:id="@+id/buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="@+id/btn_negative"
android:layout_width="0dip"
android:layout_height="44dip"
android:layout_weight="1"
android:background="@android:color/transparent"
android:gravity="center"
android:paddingTop="2dip"
android:text="取消"
android:textColor="#8e8e9c"
android:textSize="17sp"/>
android:id="@+id/vertical_divider"
style="@style/vertical_divider"/>
android:id="@+id/btn_positive"
android:layout_width="0dip"
android:layout_height="44dip"
android:layout_weight="1"
android:background="@android:color/transparent"
android:gravity="center"
android:paddingTop="2dip"
android:text="确定"
android:textColor="#f73e3e"
android:textSize="16sp"/>
7.xml布局简单效果
Paste_Image.png
再来回顾下使用方法:采用的建造者模式
Dialoger.build(getActivity())
.setContentText("你好防盗链发交电费")
.setContentTitle("标题")
.setNegativeBtn("重新认证", new Dialoger.OnNegativeListener() {
@Override public void onNegative(Dialog dialog) {
dialog.dismiss();
....
....
} })
.setPositiveBtn("确认", new Dialoger.OnPositiveListener() {
@Override public void onPositive(Dialog dialog) {
dialog.dismiss();
....//对应的操作
....
}
})
.dialoger.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
view.getViewActivity().finish();
}
}). show();
over~