Android开发教程ShowDialog工具类


已在模拟器测试运行,没发现bug。

通过本代码的演示,可以将UI相关(或其它方面)的常用操作封装成工具类,使代码复用程度更高,模块化更好,代码结构也更加清晰。

工具类UIHelper代码如下:

  1. package com.show.act;  
  2.   
  3. import Android.app.AlertDialog;  
  4. import android.app.AlertDialog.Builder;  
  5. import android.content.Context;  
  6. import android.content.DialogInterface;  
  7. import android.content.DialogInterface.OnClickListener;  
  8. import android.view.ViewGroup.LayoutParams;  
  9. import android.widget.LinearLayout;  
  10. import android.widget.TextView;  
  11.   
  12. public class UiHelper {  
  13.   
  14.     /** 
  15.      * 提问框的 Listener 
  16.      *  
  17.      * @author Lei 
  18.      */  
  19.     // 因为本类不是activity所以通过继承接口的方法获取到点击的事件   
  20.     public interface OnClickYesListener {  
  21.         abstract void onClickYes();  
  22.     }  
  23.   
  24.     /** 
  25.      * 提问框的 Listener 
  26.      *  
  27.      */  
  28.     public interface OnClickNoListener {  
  29.         abstract void onClickNo();  
  30.     }  
  31.   
  32.     public static void showQuestionDialog(Context context, String title,  
  33.             String text, final OnClickYesListener listenerYes,  
  34.             final OnClickNoListener listenerNo) {  
  35.   
  36.         Builder builder = new AlertDialog.Builder(context);  
  37.   
  38.         if (!isBlank(text)) {  
  39.             // 此方法为dialog写布局   
  40.             final TextView textView = new TextView(context);  
  41.             textView.setText(text);  
  42.             LinearLayout layout = new LinearLayout(context);  
  43.             layout.setPadding(100100);  
  44.             layout.addView(textView, new LayoutParams(LayoutParams.FILL_PARENT,  
  45.                     LayoutParams.WRAP_CONTENT));  
  46.             builder.setView(layout);  
  47.         }  
  48.         // 设置title   
  49.         builder.setTitle(title);  
  50.         // 设置确定按钮,固定用法声明一个按钮用这个setPositiveButton   
  51.         builder.setPositiveButton(context.getString(R.string.yes),  
  52.                 new OnClickListener() {  
  53.                     public void onClick(DialogInterface dialog, int which) {  
  54.                         // 如果确定被电击   
  55.                         if (listenerYes != null) {  
  56.                             listenerYes.onClickYes();  
  57.                         }  
  58.                     }  
  59.                 });  
  60.         // 设置取消按钮,固定用法声明第二个按钮要用setNegativeButton   
  61.         builder.setNegativeButton(context.getString(R.string.no),  
  62.                 new OnClickListener() {  
  63.                     public void onClick(DialogInterface dialog, int which) {  
  64.                         // 如果取消被点击   
  65.                         if (listenerNo != null) {  
  66.                             listenerNo.onClickNo();  
  67.                         }  
  68.                     }  
  69.                 });  
  70.   
  71.         // 控制这个dialog可不可以按返回键,true为可以,false为不可以   
  72.         builder.setCancelable(false);  
  73.         // 显示dialog   
  74.         builder.create().show();  
  75.   
  76.     }  
  77.   
  78.     /** 
  79.      * 处理字符的方法 
  80.      *  
  81.      * @param str 
  82.      * @return 
  83.      */  
  84.     public static boolean isBlank(String str) {  
  85.         int strLen;  
  86.         if (str == null || (strLen = str.length()) == 0) {  
  87.             return true;  
  88.         }  
  89.         for (int i = 0; i < strLen; i++) {  
  90.             if ((Character.isWhitespace(str.charAt(i)) == false)) {  
  91.                 return false;  
  92.             }  
  93.         }  
  94.         return true;  
  95.     }  
  96. }  

使用示例如下:

  1. package com.show.act;  
  2.   
  3. import com.show.act.UiHelper.OnClickNoListener;  
  4. import com.show.act.UiHelper.OnClickYesListener;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11.   
  12. public class ShowDialogActivity extends Activity {  
  13. //声明Button   
  14.   private Button showDialogButton;  
  15.     
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         init();  
  21.     }  
  22.       
  23.     private void init(){  
  24.         showDialogButton = (Button)findViewById(R.id.showDialog01);  
  25.          showDialogButton.setOnClickListener(new OnClickListener() {  
  26.               
  27.             public void onClick(View arg0) {  
  28.                   
  29.                 //调用工具类中的dialog   
  30.                 //需要传三个值到showQuestionDialog("当前界面","标题","提示内容",new 确定,new 取消 );   
  31.                 UiHelper.showQuestionDialog(ShowDialogActivity.this"提示""是否确定"new OnClickYesListener() {  
  32.                     public void onClickYes() {  
  33.                         //点击确定干什么   
  34.                           
  35.                     }  
  36.                 }, new OnClickNoListener() {  
  37.                       
  38.                     public void onClickNo() {  
  39.                         //点击取消干什么   
  40.                     }  
  41.                 });  
  42.                   
  43.             }  
  44.         });  
  45.     }  
  46. }  

相关内容