封装dialog
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了封装dialog,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3584字,纯文字阅读大概需要6分钟。
内容图文
![封装dialog](/upload/InfoBanner/zyjiaocheng/1000/aeb6ffc490b646eaaa2a1dfe3d996b89.jpg)
public class AlertDialog extends Dialog {
? ? AlertController mAlert;
? ? public AlertDialog(@NonNull Context context, @StyleRes int themeResId) {
? ? ? ? super(context, themeResId);
? ? ? ? mAlert = new AlertController(this, getWindow());
? ? }
? ? public void setText(int viewId, CharSequence text) {
? ? ? ? mAlert.setText(viewId, text);
? ? }
? ? public void setOnClickListener(int viewId, View.OnClickListener listener) {
? ? ? ? mAlert.setOnClickListener(viewId, listener);
? ? }
? ? /**
? ? ?* 减少findviewById的次数
? ? ?*/
? ? public <T extends View> T getView(int viewId) {
? ? ? ? return mAlert.getView(viewId);
? ? }
? ? public static class Builder {
? ? ? ? private final AlertController.AlertParams P;
? ? ? ? public Builder(@NonNull Context context) {
? ? ? ? ? ? this(context, R.style.dialog);
? ? ? ? }
? ? ? ? public Builder(@NonNull Context context, @StyleRes int themeResId) {
? ? ? ? ? ? P = new AlertController.AlertParams(context, themeResId);
? ? ? ? }
? ? ? ? public AlertDialog create() {
? ? ? ? ? ? final AlertDialog dialog = new AlertDialog(P.mContext, P.themeResId);
? ? ? ? ? ? P.apply(dialog.mAlert);
? ? ? ? ? ? dialog.setCancelable(P.mCancelable);
? ? ? ? ? ? if (P.mCancelable) {
? ? ? ? ? ? ? ? dialog.setCanceledOnTouchOutside(true);
? ? ? ? ? ? }
? ? ? ? ? ? dialog.getWindow().setBackgroundDrawable(new ColorDrawable());
? ? ? ? ? ? dialog.setOnCancelListener(P.mOnCancelListener);
? ? ? ? ? ? dialog.setOnDismissListener(P.mOnDismissListener);
? ? ? ? ? ? if (P.mOnKeyListener != null) {
? ? ? ? ? ? ? ? dialog.setOnKeyListener(P.mOnKeyListener);
? ? ? ? ? ? }
? ? ? ? ? ? return dialog;
? ? ? ? }
? ? ? ? /**
? ? ? ? ?* 设置View
? ? ? ? ?*/
? ? ? ? public AlertDialog.Builder setContentView(int layoutResId) {
? ? ? ? ? ? P.mView = null;
? ? ? ? ? ? P.mViewLayoutResId = layoutResId;
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? public AlertDialog.Builder setContentView(View view) {
? ? ? ? ? ? P.mView = view;
? ? ? ? ? ? P.mViewLayoutResId = 0;
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? /**
? ? ? ? ?* 存放自定义View中的文本
? ? ? ? ?*/
? ? ? ? public AlertDialog.Builder setText(int layoutId, CharSequence text) {
? ? ? ? ? ? P.mTextArray.put(layoutId, text);
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? /**
? ? ? ? ?* 存放自定义View中按钮点击事件
? ? ? ? ?*/
? ? ? ? public AlertDialog.Builder setOnClickListener(int layoutId, View.OnClickListener listener) {
? ? ? ? ? ? P.mClickArray.put(layoutId, listener);
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? //配置一些万能参数
? ? ? ? public AlertDialog.Builder fullWidth() {
? ? ? ? ? ? P.mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? //设置动画
? ? ? ? public AlertDialog.Builder fromButtom(boolean isAnimation) {
? ? ? ? ? ? if (isAnimation) {
? ? ? ? ? ? ? ? P.mAnimation = R.style.dialog_from_bottom_anim;
? ? ? ? ? ? }
? ? ? ? ? ? P.mGravity = Gravity.BOTTOM;
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? public AlertDialog.Builder setWidthAndHeight(int width, int heigth) {
? ? ? ? ? ? P.mWidth = width;
? ? ? ? ? ? P.mHeigth = heigth;
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? //设置默认动画
? ? ? ? public AlertDialog.Builder AddDefaultAnimation() {
? ? ? ? ? ? P.mAnimation = R.style.dialog_scale_anim;
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? //设置动画
? ? ? ? public AlertDialog.Builder setAnimation(int styleAnimation) {
? ? ? ? ? ? P.mAnimation = styleAnimation;
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? public AlertDialog show() {
? ? ? ? ? ? final AlertDialog dialog = create();
? ? ? ? ? ? dialog.show();
? ? ? ? ? ? return dialog;
? ? ? ? }
? ? }
}
内容总结
以上是互联网集市为您收集整理的封装dialog全部内容,希望文章能够帮你解决封装dialog所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。