Android仿IOS UIAlertView对话框

http://blog.csdn.net/zhufuing/article/details/18735371

http://blog.csdn.net/zhufuing/article/details/18735371

http://blog.csdn.net/zhufuing/article/details/18735371

http://blog.csdn.net/zhufuing/article/details/18735371

http://blog.csdn.net/zhufuing/article/details/18735371







Android仿IOS UIAlertView对话框

分类: Android app   986人阅读  评论(3)  收藏  举报

目录(?)[+]

显示效果:



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

CustomDialog使用代码:

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

自定义CustomDialog代码:

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

demo下载地址:


参考连接:

http://www.apkbus.com/forum.php?mod=viewthread&tid=160284

4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值