歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程ShowDialog工具類

Android開發教程ShowDialog工具類

日期:2017/3/1 11:17:17   编辑:Linux編程

已在模擬器測試運行,沒發現bug。

通過本代碼的演示,可以將UI相關(或其它方面)的常用操作封裝成工具類,使代碼復用程度更高,模塊化更好,代碼結構也更加清晰。

工具類UIHelper代碼如下:

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

使用示例如下:

  1. package com.show.act;
  2. import com.show.act.UiHelper.OnClickNoListener;
  3. import com.show.act.UiHelper.OnClickYesListener;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. public class ShowDialogActivity extends Activity {
  10. //聲明Button
  11. private Button showDialogButton;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. init();
  17. }
  18. private void init(){
  19. showDialogButton = (Button)findViewById(R.id.showDialog01);
  20. showDialogButton.setOnClickListener(new OnClickListener() {
  21. public void onClick(View arg0) {
  22. //調用工具類中的dialog
  23. //需要傳三個值到showQuestionDialog("當前界面","標題","提示內容",new 確定,new 取消 );
  24. UiHelper.showQuestionDialog(ShowDialogActivity.this, "提示", "是否確定", new OnClickYesListener() {
  25. public void onClickYes() {
  26. //點擊確定干什麼
  27. }
  28. }, new OnClickNoListener() {
  29. public void onClickNo() {
  30. //點擊取消干什麼
  31. }
  32. });
  33. }
  34. });
  35. }
  36. }
Copyright © Linux教程網 All Rights Reserved