Guest User

Untitled

a guest
Aug 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package com.oos.kryten.dialogs;
  2.  
  3. import static com.google.common.base.Preconditions.checkNotNull;
  4.  
  5. import android.app.Activity;
  6. import android.app.AlertDialog;
  7. import android.content.Context;
  8. import android.content.DialogInterface;
  9. import android.content.DialogInterface.OnClickListener;
  10. import android.view.KeyEvent;
  11. import android.view.View;
  12. import android.view.View.OnKeyListener;
  13. import android.widget.EditText;
  14.  
  15. public class PromptDialogBuilder extends AlertDialog.Builder {
  16.  
  17. private InternalListener listener;
  18. private CharSequence positiveText;
  19. private Integer positiveResourceId = android.R.string.ok;
  20. private CharSequence negativeText;
  21. private Integer negativeResourceId = android.R.string.cancel;
  22. private static Integer padding;
  23. private static final float PADDING = 10f;
  24.  
  25. public interface PromptListener {
  26. public void onInputProvided(String input);
  27.  
  28. public void onCancel();
  29.  
  30. public class Impl implements PromptListener {
  31.  
  32. @Override
  33. public void onInputProvided(String input) {
  34. }
  35.  
  36. @Override
  37. public void onCancel() {
  38. }
  39. }
  40.  
  41. }
  42.  
  43. @Override
  44. public AlertDialog create() {
  45. if (positiveText != null) {
  46. setPositiveButton(positiveText, listener);
  47. } else {
  48. setPositiveButton(positiveResourceId, listener);
  49. }
  50. if (negativeText != null) {
  51. setNegativeButton(negativeText, listener);
  52. } else {
  53. setNegativeButton(negativeResourceId, listener);
  54. }
  55. AlertDialog dialog = super.create();
  56. listener.input = new EditText(getContext());
  57. int pad = padding;
  58. listener.input.setOnKeyListener(listener);
  59. dialog.setView(listener.input, pad, 0, pad, pad);
  60. return dialog;
  61. }
  62.  
  63. public PromptDialogBuilder(Activity activity) {
  64. super(activity);
  65. Display display = activity.getWindowManager().getDefaultDisplay();
  66. DisplayMetrics metrics = new DisplayMetrics();
  67. display.getMetrics(metrics);
  68. padding = (int) (PADDING * metrics.density);
  69. listener = new InternalListener(new PromptListener.Impl());
  70. }
  71.  
  72. public PromptDialogBuilder setPromptListener(PromptListener listener) {
  73. this.listener = new InternalListener(listener);
  74. return this;
  75. }
  76.  
  77. public PromptDialogBuilder setPositiveButton(CharSequence text) {
  78. positiveText = text;
  79. return this;
  80. }
  81.  
  82. public PromptDialogBuilder setPositiveButton(int textId) {
  83. positiveResourceId = textId;
  84. return this;
  85. }
  86.  
  87. public PromptDialogBuilder setNegativeButton(CharSequence text) {
  88. negativeText = text;
  89. return this;
  90. }
  91.  
  92. public PromptDialogBuilder setNegativeButton(int textId) {
  93. negativeResourceId = textId;
  94. return this;
  95. }
  96.  
  97. private class InternalListener implements OnClickListener, OnKeyListener {
  98.  
  99. private final PromptListener listener;
  100. private EditText input;
  101.  
  102. private InternalListener(PromptListener listener) {
  103. super();
  104. if(listener == null) {
  105. throw new NullPointerException("Listener can't be null");
  106. }
  107. this.listener = listener;
  108. }
  109.  
  110. @Override
  111. public void onClick(DialogInterface dialog, int which) {
  112. switch (which) {
  113. case AlertDialog.BUTTON_POSITIVE:
  114. final CharSequence email = input.getText();
  115. listener.onEmailProvided(email.toString());
  116. case AlertDialog.BUTTON_NEGATIVE:
  117. default:
  118. dialog.dismiss();
  119. break;
  120. }
  121. }
  122.  
  123. @Override
  124. public boolean onKey(View v, int keyCode, KeyEvent event) {
  125. if (keyCode == KeyEvent.KEYCODE_ENTER) {
  126. onClick(dialog, AlertDialog.BUTTON_POSITIVE);
  127. return true;
  128. }
  129. return false;
  130. }
  131. }
  132.  
  133. }
Add Comment
Please, Sign In to add comment