Android仿IOS UIAlertView对话框

显示效果:

        我在参考链接中看到了作者的仿的qq提示框,但是在使用的时候并不是很方面,有一些不足,于是我参照Android系统AlertDialog,使用参考链接中的布局文件和style文件,用自己的方法自定义了一下这个仿IOS上面UIAlertView的效果,这样的话让我们可以想使用系统AlertDialog一样使用我自定义的CustomDialog。

CustomDialog使用代码:

[java]
  1. package com.example.iosalertview;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import com.example.iosalertview.CustomDialog.Builder;
  9. public class MainActivity extends Activity implements OnClickListener{
  10.     private Button ios_dialog_btn,android_dialog_btn;
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         ios_dialog_btn = (Button) findViewById(R.id.ios_dialog_btn);
  16.         android_dialog_btn = (Button) findViewById(R.id.android_dialog_btn);
  17.         ios_dialog_btn.setOnClickListener(this);
  18.         android_dialog_btn.setOnClickListener(this);
  19.     }
  20.     @Override
  21.     public void onClick(View v) {
  22.         switch (v.getId()) {
  23.         case R.id.ios_dialog_btn:
  24.             CustomDialog.Builder builder = new Builder(MainActivity.this);
  25.             builder.setTitle(R.string.prompt);
  26.             builder.setMessage(R.string.exit_app);
  27.             builder.setPositiveButton(R.string.confirm, null);
  28.             builder.setNegativeButton(R.string.cancel, null);
  29.             builder.create().show();
  30.             break;
  31.         case R.id.android_dialog_btn:
  32.             AlertDialog.Builder mbuilder = new AlertDialog.Builder(MainActivity.this);
  33.             mbuilder.setTitle(R.string.prompt);
  34.             mbuilder.setMessage(R.string.exit_app);
  35.             mbuilder.setPositiveButton(R.string.confirm, null);
  36.             mbuilder.setNegativeButton(R.string.cancel, null);
  37.             mbuilder.create().show();
  38.             break;
  39.         default:
  40.             break;
  41.         }
  42.     }
  43. }

自定义CustomDialog代码:

[java] 
  1. package com.example.iosalertview;
  2. import android.app.Dialog;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup.LayoutParams;
  8. import android.widget.Button;
  9. import android.widget.LinearLayout;
  10. import android.widget.TextView;
  11. public class CustomDialog extends Dialog {
  12.     public CustomDialog(Context context) {
  13.         super(context);
  14.     }
  15.     public CustomDialog(Context context, int theme) {
  16.         super(context, theme);
  17.     }
  18.     public static class Builder {
  19.         private Context context; //上下文对象
  20.         private String title; //对话框标题
  21.         private String message; //对话框内容
  22.         private String confirm_btnText; //按钮名称“确定”
  23.         private String cancel_btnText; //按钮名称“取消”
  24.         private View contentView; //对话框中间加载的其他布局界面
  25.         /*按钮坚挺事件*/
  26.         private DialogInterface.OnClickListener confirm_btnClickListener;
  27.         private DialogInterface.OnClickListener cancel_btnClickListener;
  28.         public Builder(Context context) {
  29.             this.context = context;
  30.         }
  31.         /*设置对话框信息*/
  32.         public Builder setMessage(String message) {
  33.             this.message = message;
  34.             return this;
  35.         }
  36.         /**
  37.          * Set the Dialog message from resource
  38.          *
  39.          * @param title
  40.          * @return
  41.          */
  42.         public Builder setMessage(int message) {
  43.             this.message = (String) context.getText(message);
  44.             return this;
  45.         }
  46.         /**
  47.          * Set the Dialog title from resource
  48.          *
  49.          * @param title
  50.          * @return
  51.          */
  52.         public Builder setTitle(int title) {
  53.             this.title = (String) context.getText(title);
  54.             return this;
  55.         }
  56.         /**
  57.          * Set the Dialog title from String
  58.          *
  59.          * @param title
  60.          * @return
  61.          */
  62.         public Builder setTitle(String title) {
  63.             this.title = title;
  64.             return this;
  65.         }
  66.         /**
  67.          * 设置对话框界面
  68.          * @param v View
  69.          * @return
  70.          */
  71.         public Builder setContentView(View v) {
  72.             this.contentView = v;
  73.             return this;
  74.         }
  75.         /**
  76.          * Set the positive button resource and it’s listener
  77.          *
  78.          * @param confirm_btnText
  79.          * @return
  80.          */
  81.         public Builder setPositiveButton(int confirm_btnText,
  82.                 DialogInterface.OnClickListener listener) {
  83.             this.confirm_btnText = (String) context
  84.                     .getText(confirm_btnText);
  85.             this.confirm_btnClickListener = listener;
  86.             return this;
  87.         }
  88.         /**
  89.          * Set the positive button and it’s listener
  90.          *
  91.          * @param confirm_btnText
  92.          * @return
  93.          */
  94.         public Builder setPositiveButton(String confirm_btnText,
  95.                 DialogInterface.OnClickListener listener) {
  96.             this.confirm_btnText = confirm_btnText;
  97.             this.confirm_btnClickListener = listener;
  98.             return this;
  99.         }
  100.         /**
  101.          * Set the negative button resource and it’s listener
  102.          *
  103.          * @param confirm_btnText
  104.          * @return
  105.          */
  106.         public Builder setNegativeButton(int cancel_btnText,
  107.                 DialogInterface.OnClickListener listener) {
  108.             this.cancel_btnText = (String) context
  109.                     .getText(cancel_btnText);
  110.             this.cancel_btnClickListener = listener;
  111.             return this;
  112.         }
  113.         /**
  114.          * Set the negative button and it’s listener
  115.          *
  116.          * @param confirm_btnText
  117.          * @return
  118.          */
  119.         public Builder setNegativeButton(String cancel_btnText,
  120.                 DialogInterface.OnClickListener listener) {
  121.             this.cancel_btnText = cancel_btnText;
  122.             this.cancel_btnClickListener = listener;
  123.             return this;
  124.         }
  125.         public CustomDialog create() {
  126.             LayoutInflater inflater = (LayoutInflater) context
  127.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  128.             // instantiate the dialog with the custom Theme
  129.             final CustomDialog dialog = new CustomDialog(context, R.style.mystyle);
  130.             View layout = inflater.inflate(R.layout.customdialog, null);
  131.             dialog.addContentView(layout, new LayoutParams(
  132.                     LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  133.             // set the dialog title
  134.             ((TextView) layout.findViewById(R.id.title)).setText(title);
  135.             ((TextView) layout.findViewById(R.id.title)).getPaint().setFakeBoldText(true);;
  136.             // set the confirm button
  137.             if (confirm_btnText != null) {
  138.                 ((Button) layout.findViewById(R.id.confirm_btn))
  139.                         .setText(confirm_btnText);
  140.                 if (confirm_btnClickListener != null) {
  141.                     ((Button) layout.findViewById(R.id.confirm_btn))
  142.                             .setOnClickListener(new View.OnClickListener() {
  143.                                 public void onClick(View v) {
  144.                                     confirm_btnClickListener.onClick(dialog,
  145.                                             DialogInterface.BUTTON_POSITIVE);
  146.                                 }
  147.                             });
  148.                 }
  149.             } else {
  150.                 // if no confirm button just set the visibility to GONE
  151.                 layout.findViewById(R.id.confirm_btn).setVisibility(
  152.                         View.GONE);
  153.             }
  154.             // set the cancel button
  155.             if (cancel_btnText != null) {
  156.                 ((Button) layout.findViewById(R.id.cancel_btn))
  157.                         .setText(cancel_btnText);
  158.                 if (cancel_btnClickListener != null) {
  159.                     ((Button) layout.findViewById(R.id.cancel_btn))
  160.                             .setOnClickListener(new View.OnClickListener() {
  161.                                 public void onClick(View v) {
  162.                                     cancel_btnClickListener.onClick(dialog,
  163.                                             DialogInterface.BUTTON_NEGATIVE);
  164.                                 }
  165.                             });
  166.                 }
  167.             } else {
  168.                 // if no confirm button just set the visibility to GONE
  169.                 layout.findViewById(R.id.cancel_btn).setVisibility(
  170.                         View.GONE);
  171.             }
  172.             // set the content message
  173.             if (message != null) {
  174.                 ((TextView) layout.findViewById(R.id.message)).setText(message);
  175.             } else if (contentView != null) {
  176.                 // if no message set
  177.                 // add the contentView to the dialog body
  178.                 ((LinearLayout) layout.findViewById(R.id.message))
  179.                         .removeAllViews();
  180.                 ((LinearLayout) layout.findViewById(R.id.message)).addView(
  181.                         contentView, new LayoutParams(
  182.                                 LayoutParams.WRAP_CONTENT,
  183.                                 LayoutParams.WRAP_CONTENT));
  184.             }
  185.             dialog.setContentView(layout);
  186.             return dialog;
  187.         }
  188.     }
  189. }

demo下载地址:

http://download.csdn.net/detail/zhufuing/6880735

标签