自定义Dialog之信息提示
16lz
2021-01-26
对话框对于应用也是必不可少的一个组件,在Android中也不例外,对话框对于一些提示重要信息,或者一些需要用户额外交互的一些内容很有帮助。
自定义Dialog步骤:
1、主要创建Java类,并继承Dialog
2、创建布局文件来加载和一些样式文件
效果图:
DialogBox.java
Java代码- public class DialogBox extends Dialog {
- public DialogBox(Context context) {
- super(context);
- }
- public DialogBox(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 title
- * @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 DialogBox create() {
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- final DialogBox dialog = new DialogBox(context, R.style.Dialog);
- View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
- dialog.addContentView(layout, new LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
- ((TextView) layout.findViewById(R.id.title)).setText(title);
- if (positiveButtonText != null) {
- ((Button) layout.findViewById(R.id.positiveButton))
- .setText(positiveButtonText);
- if (positiveButtonClickListener != null) {
- ((Button) layout.findViewById(R.id.positiveButton))
- .setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- positiveButtonClickListener.onClick(dialog,
- DialogInterface.BUTTON_POSITIVE);
- }
- });
- }
- } else {
- layout.findViewById(R.id.positiveButton).setVisibility(
- View.GONE);
- }
- if (negativeButtonText != null) {
- ((Button) layout.findViewById(R.id.negativeButton))
- .setText(negativeButtonText);
- if (negativeButtonClickListener != null) {
- ((Button) layout.findViewById(R.id.negativeButton))
- .setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- negativeButtonClickListener.onClick(dialog,
- DialogInterface.BUTTON_NEGATIVE);
- }
- });
- }
- } else {
- layout.findViewById(R.id.negativeButton).setVisibility(
- View.GONE);
- }
- if (message != null) {
- ((TextView) layout.findViewById(R.id.message)).setText(message);
- } else if (contentView != null) {
- ((LinearLayout) layout.findViewById(R.id.content))
- .removeAllViews();
- ((LinearLayout) layout.findViewById(R.id.content)).addView(
- contentView, new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.FILL_PARENT));
- }
- dialog.setContentView(layout);
- return dialog;
- }
- }
- }
自定义View的布局文件:
<?xml version="1.0" encoding="utf-8"?>
MainActivity.Java
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void showAlertDialog(View view) {DialogBox.Builder builder = new DialogBox.Builder(this);builder.setMessage("删除该商品?");builder.setTitle("温馨提示");builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {dialog.dismiss();// 你的操作事项}});builder.setNegativeButton("取消",new android.content.DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});builder.create().show();}}
源码点击下载
更多相关文章
- android support Percent支持库开发
- Mars视频笔记——Animation(2)
- Hello Android(安卓)- File文件操作
- Ubuntu14.04 下面安装SVN服务器
- android部分目录分析
- 【Android】更改程序图标
- Android实习生 —— 数据存储与共享
- Android开发——Android(安卓)Studio下使用Cmake在NDK环境下移植
- Android(安卓)Support Percent百分比布局