|
1 package org.hedgewars.hedgeroid.util; |
|
2 |
|
3 import android.app.Activity; |
|
4 import android.app.AlertDialog; |
|
5 import android.app.Dialog; |
|
6 import android.content.DialogInterface; |
|
7 import android.os.Bundle; |
|
8 import android.support.v4.app.DialogFragment; |
|
9 import android.view.KeyEvent; |
|
10 import android.view.inputmethod.EditorInfo; |
|
11 import android.widget.EditText; |
|
12 import android.widget.TextView; |
|
13 import android.widget.TextView.OnEditorActionListener; |
|
14 |
|
15 /** |
|
16 * A generic text input dialog with configurable text. The Activity must implement the callback |
|
17 * interface TextInputDialogListener, which will be called by the dialog if it is submitted or cancelled. |
|
18 */ |
|
19 public class TextInputDialog extends DialogFragment { |
|
20 private static final String BUNDLE_DIALOG_ID = "dialogId"; |
|
21 private static final String BUNDLE_TITLE_TEXT = "title"; |
|
22 private static final String BUNDLE_MESSAGE_TEXT = "message"; |
|
23 private static final String BUNDLE_HINT_TEXT = "hint"; |
|
24 |
|
25 private int dialogId, titleText, messageText, hintText; |
|
26 private TextInputDialogListener listener; |
|
27 |
|
28 public interface TextInputDialogListener { |
|
29 void onTextInputDialogSubmitted(int dialogId, String text); |
|
30 void onTextInputDialogCancelled(int dialogId); |
|
31 } |
|
32 |
|
33 /** |
|
34 * The dialogId is only used for passing back to the callback on the activity, the |
|
35 * other parameters are text resource IDs. Pass 0 for any of them to not use this |
|
36 * text. |
|
37 */ |
|
38 public TextInputDialog(int dialogId, int titleText, int messageText, int hintText) { |
|
39 this.dialogId = dialogId; |
|
40 this.titleText = titleText; |
|
41 this.messageText = messageText; |
|
42 this.hintText = hintText; |
|
43 } |
|
44 |
|
45 public TextInputDialog() { |
|
46 // Only for reflection-based instantiation by the framework |
|
47 } |
|
48 |
|
49 @Override |
|
50 public void onAttach(Activity activity) { |
|
51 super.onAttach(activity); |
|
52 try { |
|
53 listener = (TextInputDialogListener) activity; |
|
54 } catch(ClassCastException e) { |
|
55 throw new ClassCastException("Activity " + activity + " must implement TextInputDialogListener to use TextInputDialog."); |
|
56 } |
|
57 } |
|
58 |
|
59 @Override |
|
60 public void onDetach() { |
|
61 super.onDetach(); |
|
62 listener = null; |
|
63 } |
|
64 |
|
65 @Override |
|
66 public Dialog onCreateDialog(Bundle savedInstanceState) { |
|
67 if(savedInstanceState != null) { |
|
68 dialogId = savedInstanceState.getInt(BUNDLE_DIALOG_ID, dialogId); |
|
69 titleText = savedInstanceState.getInt(BUNDLE_TITLE_TEXT, titleText); |
|
70 messageText = savedInstanceState.getInt(BUNDLE_MESSAGE_TEXT, messageText); |
|
71 hintText = savedInstanceState.getInt(BUNDLE_HINT_TEXT, hintText); |
|
72 } |
|
73 |
|
74 final EditText editText = new EditText(getActivity()); |
|
75 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
|
76 |
|
77 if(titleText != 0) { |
|
78 builder.setTitle(titleText); |
|
79 } |
|
80 if(messageText != 0) { |
|
81 builder.setTitle(messageText); |
|
82 } |
|
83 if(hintText != 0) { |
|
84 editText.setHint(hintText); |
|
85 } |
|
86 |
|
87 editText.setId(android.R.id.text1); |
|
88 editText.setImeOptions(EditorInfo.IME_ACTION_DONE); |
|
89 editText.setSingleLine(); |
|
90 |
|
91 builder.setView(editText); |
|
92 builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { |
|
93 public void onClick(DialogInterface dialog, int which) { |
|
94 dialog.cancel(); |
|
95 } |
|
96 }); |
|
97 |
|
98 editText.setOnEditorActionListener(new OnEditorActionListener() { |
|
99 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
|
100 listener.onTextInputDialogSubmitted(dialogId, v.getText().toString()); |
|
101 return true; |
|
102 } |
|
103 }); |
|
104 |
|
105 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
|
106 public void onClick(DialogInterface dialog, int which) { |
|
107 listener.onTextInputDialogSubmitted(dialogId, editText.getText().toString()); |
|
108 } |
|
109 }); |
|
110 |
|
111 return builder.create(); |
|
112 } |
|
113 |
|
114 @Override |
|
115 public void onSaveInstanceState(Bundle icicle) { |
|
116 super.onSaveInstanceState(icicle); |
|
117 icicle.putInt(BUNDLE_DIALOG_ID, dialogId); |
|
118 icicle.putInt(BUNDLE_TITLE_TEXT, titleText); |
|
119 icicle.putInt(BUNDLE_MESSAGE_TEXT, messageText); |
|
120 icicle.putInt(BUNDLE_HINT_TEXT, hintText); |
|
121 } |
|
122 |
|
123 @Override |
|
124 public void onCancel(DialogInterface dialog) { |
|
125 super.onCancel(dialog); |
|
126 listener.onTextInputDialogCancelled(dialogId); |
|
127 } |
|
128 } |