自定义Dialog
需要实现的效果图:
成都创新互联是一家专注于网站设计制作、成都做网站与策划设计,沙河网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:沙河等地区。沙河做网站价格咨询:13518219792
一个项目所用到的对话框风格必须统一,但显示的文字、布局却可以不同。如果每个对话框都要重新去创建的话,会增加代码的冗余,浪费资源。所以可以写个类 MyDialog 继承Dailog。需要的时候直接调用MyDialog类。
一、新建xml布局。
xml效果图如下:
尽量将对话框要用到的所有控件在xml中排好,部分不需要的可以在代码setVisibility(View.GONE);让其消失,省的新建多个xml文件。
二、继承Dailog
package com.dnake.evertalk.widgets; import com.dnake.evertalk.R; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; public class MyDialog extends Dialog { public MyDialog(Context context) { super(context); } public MyDialog(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; } public Builder setMessage(int message) {//设置信息 this.message = (String) context.getText(message); return this; } public Builder setTitle(int title) { //设置标题 this.title = (String) context.getText(title); return this; } 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 */ 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 void create(View layout) { final MyDialog dialog = new MyDialog(context,R.style.dialog_theme); dialog.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT, 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(message != null) { TextView hint = (TextView) layout.findViewById(R.id.tv_message); hint.setText(message); } 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); } dialog.setContentView(layout); dialog.show(); } } }
三、在Activity中创建dialog,设置标题、信息以及按钮的处理事件
private void showDialog() { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View layout = inflater.inflate(R.layout.dialog, null); Builder builder = new MyDialog.Builder(this); builder.setTitle(getResources().getString(R.string.mymessageactivity_delete_title)); builder.setMessage(getResources().getString(R.string.mymessageactivity_delete_all_hint)); builder.setPositiveButton(getResources().getString(R.string.sure),new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); intData(); } }); builder.setNegativeButton(getResources().getString(R.string.cancel), new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create(layout); }
这样就实现了项目需要的对话框效果图,且不同的Activity的对话框可显示不同的文字,也可以处理不同的点击事件。
本文标题:自定义Dialog
分享URL:http://hbruida.cn/article/ghsogj.html