Windows Python java centos shell Ubuntu php nginx 开源 wordpress 微软 apache Android mysql linux google Firefox 程序员 云计算 编程

Android自定義Dialog(美化界面)

主要代码:

 

  1. /** 
  2.  * 自定义dialog 
  3.  * @author ansen 
  4.  */  
  5. public class CustomDialog extends Dialog {  
  6.     public CustomDialog(Context context) {  
  7.         super(context);  
  8.     }  
  9.   
  10.     public CustomDialog(Context context, int theme) {  
  11.         super(context, theme);  
  12.     }  
  13.   
  14.     public static class Builder {  
  15.         private Context context;  
  16.         private String title;  
  17.         private String message;  
  18.         private String positiveButtonText;  
  19.         private String negativeButtonText;  
  20.         private View contentView;  
  21.         private DialogInterface.OnClickListener positiveButtonClickListener;  
  22.         private DialogInterface.OnClickListener negativeButtonClickListener;  
  23.   
  24.         public Builder(Context context) {  
  25.             this.context = context;  
  26.         }  
  27.   
  28.         public Builder setMessage(String message) {  
  29.             this.message = message;  
  30.             return this;  
  31.         }  
  32.   
  33.         /** 
  34.          * set the Dialog message from resource 
  35.          * @param title 
  36.          * @return 
  37.          */  
  38.         public Builder setMessage(int message) {  
  39.             this.message = (String) context.getText(message);  
  40.             return this;  
  41.         }  
  42.   
  43.         /** 
  44.          * Set the Dialog title from resource 
  45.          * @param title 
  46.          * @return 
  47.          */  
  48.         public Builder setTitle(int title) {  
  49.             this.title = (String) context.getText(title);  
  50.             return this;  
  51.         }  
  52.   
  53.         /** 
  54.          * Set the Dialog title from String 
  55.          * @param title 
  56.          * @return 
  57.          */  
  58.   
  59.         public Builder setTitle(String title) {  
  60.             this.title = title;  
  61.             return this;  
  62.         }  
  63.   
  64.         public Builder setContentView(View v) {  
  65.             this.contentView = v;  
  66.             return this;  
  67.         }  
  68.   
  69.         /** 
  70.          * Set the positive button resource and it's listener 
  71.          * @param positiveButtonText 
  72.          * @return 
  73.          */  
  74.         public Builder setPositiveButton(int positiveButtonText,  
  75.                 DialogInterface.OnClickListener listener) {  
  76.             this.positiveButtonText = (String) context  
  77.                     .getText(positiveButtonText);  
  78.             this.positiveButtonClickListener = listener;  
  79.             return this;  
  80.         }  
  81.   
  82.         public Builder setPositiveButton(String positiveButtonText,  
  83.                 DialogInterface.OnClickListener listener) {  
  84.             this.positiveButtonText = positiveButtonText;  
  85.             this.positiveButtonClickListener = listener;  
  86.             return this;  
  87.         }  
  88.   
  89.         public Builder setNegativeButton(int negativeButtonText,  
  90.                 DialogInterface.OnClickListener listener) {  
  91.             this.negativeButtonText = (String) context  
  92.                     .getText(negativeButtonText);  
  93.             this.negativeButtonClickListener = listener;  
  94.             return this;  
  95.         }  
  96.   
  97.         public Builder setNegativeButton(String negativeButtonText,  
  98.                 DialogInterface.OnClickListener listener) {  
  99.             this.negativeButtonText = negativeButtonText;  
  100.             this.negativeButtonClickListener = listener;  
  101.             return this;  
  102.         }  
  103.           
  104.         //创建dialog对象   主要就是这个方法,加载自定义布局文件  
  105.         public CustomDialog create() {  
  106.             LayoutInflater inflater = (LayoutInflater) context  
  107.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  108.             // instantiate the dialog with the custom Theme  
  109.             final CustomDialog dialog = new CustomDialog(context,  
  110.                     R.style.Dialog);  
  111.             View layout = inflater.inflate(R.layout.dialog_normal_layout, null);  
  112.             dialog.addContentView(layout, new LayoutParams(  
  113.                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  114.             // set the dialog title  
  115.             ((TextView) layout.findViewById(R.id.title)).setText(title);  
  116.             // set the confirm button  
  117.             if (positiveButtonText != null) {  
  118.                 ((Button) layout.findViewById(R.id.positiveButton))  
  119.                         .setText(positiveButtonText);  
  120.                 if (positiveButtonClickListener != null) {  
  121.                     ((Button) layout.findViewById(R.id.positiveButton))  
  122.                             .setOnClickListener(new View.OnClickListener() {  
  123.                                 public void onClick(View v) {  
  124.                                     positiveButtonClickListener.onClick(dialog,  
  125.                                             DialogInterface.BUTTON_POSITIVE);  
  126.                                 }  
  127.                             });  
  128.                 }  
  129.             } else {  
  130.                 // if no confirm button just set the visibility to GONE  
  131.                 layout.findViewById(R.id.positiveButton).setVisibility(  
  132.                         View.GONE);  
  133.             }  
  134.             // set the cancel button  
  135.             if (negativeButtonText != null) {  
  136.                 ((Button) layout.findViewById(R.id.negativeButton))  
  137.                         .setText(negativeButtonText);  
  138.                 if (negativeButtonClickListener != null) {  
  139.                     ((Button) layout.findViewById(R.id.negativeButton))  
  140.                             .setOnClickListener(new View.OnClickListener() {  
  141.                                 public void onClick(View v) {  
  142.                                     negativeButtonClickListener.onClick(dialog,  
  143.                                             DialogInterface.BUTTON_NEGATIVE);  
  144.                                 }  
  145.                             });  
  146.                 }  
  147.             } else {  
  148.                 // if no confirm button just set the visibility to GONE  
  149.                 layout.findViewById(R.id.negativeButton).setVisibility(  
  150.                         View.GONE);  
  151.             }  
  152.             // set the content message  
  153.             if (message != null) {  
  154.                 ((TextView) layout.findViewById(R.id.message)).setText(message);  
  155.             } else if (contentView != null) {  
  156.                 ((LinearLayout) layout.findViewById(R.id.content))  
  157.                         .removeAllViews();  
  158.                 ((LinearLayout) layout.findViewById(R.id.content)).addView(  
  159.                         contentView, new LayoutParams(LayoutParams.FILL_PARENT,  
  160.                                 LayoutParams.FILL_PARENT));  
  161.             }  
  162.             dialog.setContentView(layout);  
  163.             return dialog;  
  164.         }  
  165.   
  166.     }  
  167. }  

 

4.Activity中如何调用:

 

  1. CustomDialog.Builder builder = new CustomDialog.Builder(this);  
  2. builder.setMessage("这个就是自定义的提示框");  
  3. builder.setTitle("提示");  
  4. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  5.     public void onClick(DialogInterface dialog, int which) {  
  6.         dialog.dismiss();  
  7.         //设置你的操作事项  
  8.     }  
  9. });  
  10.   
  11. builder.setNegativeButton("取消",  
  12.         new android.content.DialogInterface.OnClickListener() {  
  13.             public void onClick(DialogInterface dialog, int which) {  
  14.                 dialog.dismiss();  
  15.             }  
  16.         });  
  17.   
  18. builder.create().show();  

延伸阅读

评论