34 /** |
34 /** |
35 * A generic text input dialog with configurable text. The Activity must implement the callback |
35 * A generic text input dialog with configurable text. The Activity must implement the callback |
36 * interface TextInputDialogListener, which will be called by the dialog if it is submitted or cancelled. |
36 * interface TextInputDialogListener, which will be called by the dialog if it is submitted or cancelled. |
37 */ |
37 */ |
38 public class TextInputDialog extends DialogFragment { |
38 public class TextInputDialog extends DialogFragment { |
39 private static final String BUNDLE_DIALOG_ID = "dialogId"; |
39 private static final String BUNDLE_DIALOG_ID = "dialogId"; |
40 private static final String BUNDLE_TITLE_TEXT = "title"; |
40 private static final String BUNDLE_TITLE_TEXT = "title"; |
41 private static final String BUNDLE_MESSAGE_TEXT = "message"; |
41 private static final String BUNDLE_MESSAGE_TEXT = "message"; |
42 private static final String BUNDLE_HINT_TEXT = "hint"; |
42 private static final String BUNDLE_HINT_TEXT = "hint"; |
43 |
|
44 private int dialogId, titleText, messageText, hintText; |
|
45 private TextInputDialogListener listener; |
|
46 |
|
47 public interface TextInputDialogListener { |
|
48 void onTextInputDialogSubmitted(int dialogId, String text); |
|
49 void onTextInputDialogCancelled(int dialogId); |
|
50 } |
|
51 |
|
52 /** |
|
53 * The dialogId is only used for passing back to the callback on the activity, the |
|
54 * other parameters are text resource IDs. Pass 0 for any of them to not use this |
|
55 * text. |
|
56 */ |
|
57 public TextInputDialog(int dialogId, int titleText, int messageText, int hintText) { |
|
58 this.dialogId = dialogId; |
|
59 this.titleText = titleText; |
|
60 this.messageText = messageText; |
|
61 this.hintText = hintText; |
|
62 } |
|
63 |
|
64 public TextInputDialog() { |
|
65 // Only for reflection-based instantiation by the framework |
|
66 } |
|
67 |
|
68 @Override |
|
69 public void onAttach(Activity activity) { |
|
70 super.onAttach(activity); |
|
71 try { |
|
72 listener = (TextInputDialogListener) activity; |
|
73 } catch(ClassCastException e) { |
|
74 throw new ClassCastException("Activity " + activity + " must implement TextInputDialogListener to use TextInputDialog."); |
|
75 } |
|
76 } |
|
77 |
|
78 @Override |
|
79 public void onDetach() { |
|
80 super.onDetach(); |
|
81 listener = null; |
|
82 } |
|
83 |
|
84 @Override |
|
85 public Dialog onCreateDialog(Bundle savedInstanceState) { |
|
86 if(savedInstanceState != null) { |
|
87 dialogId = savedInstanceState.getInt(BUNDLE_DIALOG_ID, dialogId); |
|
88 titleText = savedInstanceState.getInt(BUNDLE_TITLE_TEXT, titleText); |
|
89 messageText = savedInstanceState.getInt(BUNDLE_MESSAGE_TEXT, messageText); |
|
90 hintText = savedInstanceState.getInt(BUNDLE_HINT_TEXT, hintText); |
|
91 } |
|
92 |
|
93 final EditText editText = new EditText(getActivity()); |
|
94 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
|
95 |
|
96 if(titleText != 0) { |
|
97 builder.setTitle(titleText); |
|
98 } |
|
99 if(messageText != 0) { |
|
100 builder.setTitle(messageText); |
|
101 } |
|
102 if(hintText != 0) { |
|
103 editText.setHint(hintText); |
|
104 } |
|
105 |
|
106 editText.setId(android.R.id.text1); |
|
107 editText.setImeOptions(EditorInfo.IME_ACTION_DONE); |
|
108 editText.setSingleLine(); |
|
109 |
43 |
110 builder.setView(editText); |
44 private int dialogId, titleText, messageText, hintText; |
111 builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { |
45 private TextInputDialogListener listener; |
112 public void onClick(DialogInterface dialog, int which) { |
|
113 dialog.cancel(); |
|
114 } |
|
115 }); |
|
116 |
|
117 editText.setOnEditorActionListener(new OnEditorActionListener() { |
|
118 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
|
119 listener.onTextInputDialogSubmitted(dialogId, v.getText().toString()); |
|
120 dismiss(); |
|
121 return true; |
|
122 } |
|
123 }); |
|
124 |
|
125 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
|
126 public void onClick(DialogInterface dialog, int which) { |
|
127 listener.onTextInputDialogSubmitted(dialogId, editText.getText().toString()); |
|
128 } |
|
129 }); |
|
130 |
46 |
131 return builder.create(); |
47 public interface TextInputDialogListener { |
132 } |
48 void onTextInputDialogSubmitted(int dialogId, String text); |
133 |
49 void onTextInputDialogCancelled(int dialogId); |
134 @Override |
50 } |
135 public void onSaveInstanceState(Bundle icicle) { |
51 |
136 super.onSaveInstanceState(icicle); |
52 /** |
137 icicle.putInt(BUNDLE_DIALOG_ID, dialogId); |
53 * The dialogId is only used for passing back to the callback on the activity, the |
138 icicle.putInt(BUNDLE_TITLE_TEXT, titleText); |
54 * other parameters are text resource IDs. Pass 0 for any of them to not use this |
139 icicle.putInt(BUNDLE_MESSAGE_TEXT, messageText); |
55 * text. |
140 icicle.putInt(BUNDLE_HINT_TEXT, hintText); |
56 */ |
141 } |
57 public TextInputDialog(int dialogId, int titleText, int messageText, int hintText) { |
142 |
58 this.dialogId = dialogId; |
143 @Override |
59 this.titleText = titleText; |
144 public void onCancel(DialogInterface dialog) { |
60 this.messageText = messageText; |
145 super.onCancel(dialog); |
61 this.hintText = hintText; |
146 listener.onTextInputDialogCancelled(dialogId); |
62 } |
147 } |
63 |
|
64 public TextInputDialog() { |
|
65 // Only for reflection-based instantiation by the framework |
|
66 } |
|
67 |
|
68 @Override |
|
69 public void onAttach(Activity activity) { |
|
70 super.onAttach(activity); |
|
71 try { |
|
72 listener = (TextInputDialogListener) activity; |
|
73 } catch(ClassCastException e) { |
|
74 throw new ClassCastException("Activity " + activity + " must implement TextInputDialogListener to use TextInputDialog."); |
|
75 } |
|
76 } |
|
77 |
|
78 @Override |
|
79 public void onDetach() { |
|
80 super.onDetach(); |
|
81 listener = null; |
|
82 } |
|
83 |
|
84 @Override |
|
85 public Dialog onCreateDialog(Bundle savedInstanceState) { |
|
86 if(savedInstanceState != null) { |
|
87 dialogId = savedInstanceState.getInt(BUNDLE_DIALOG_ID, dialogId); |
|
88 titleText = savedInstanceState.getInt(BUNDLE_TITLE_TEXT, titleText); |
|
89 messageText = savedInstanceState.getInt(BUNDLE_MESSAGE_TEXT, messageText); |
|
90 hintText = savedInstanceState.getInt(BUNDLE_HINT_TEXT, hintText); |
|
91 } |
|
92 |
|
93 final EditText editText = new EditText(getActivity()); |
|
94 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
|
95 |
|
96 if(titleText != 0) { |
|
97 builder.setTitle(titleText); |
|
98 } |
|
99 if(messageText != 0) { |
|
100 builder.setTitle(messageText); |
|
101 } |
|
102 if(hintText != 0) { |
|
103 editText.setHint(hintText); |
|
104 } |
|
105 |
|
106 editText.setId(android.R.id.text1); |
|
107 editText.setImeOptions(EditorInfo.IME_ACTION_DONE); |
|
108 editText.setSingleLine(); |
|
109 |
|
110 builder.setView(editText); |
|
111 builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { |
|
112 public void onClick(DialogInterface dialog, int which) { |
|
113 dialog.cancel(); |
|
114 } |
|
115 }); |
|
116 |
|
117 editText.setOnEditorActionListener(new OnEditorActionListener() { |
|
118 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
|
119 listener.onTextInputDialogSubmitted(dialogId, v.getText().toString()); |
|
120 dismiss(); |
|
121 return true; |
|
122 } |
|
123 }); |
|
124 |
|
125 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
|
126 public void onClick(DialogInterface dialog, int which) { |
|
127 listener.onTextInputDialogSubmitted(dialogId, editText.getText().toString()); |
|
128 } |
|
129 }); |
|
130 |
|
131 return builder.create(); |
|
132 } |
|
133 |
|
134 @Override |
|
135 public void onSaveInstanceState(Bundle icicle) { |
|
136 super.onSaveInstanceState(icicle); |
|
137 icicle.putInt(BUNDLE_DIALOG_ID, dialogId); |
|
138 icicle.putInt(BUNDLE_TITLE_TEXT, titleText); |
|
139 icicle.putInt(BUNDLE_MESSAGE_TEXT, messageText); |
|
140 icicle.putInt(BUNDLE_HINT_TEXT, hintText); |
|
141 } |
|
142 |
|
143 @Override |
|
144 public void onCancel(DialogInterface dialog) { |
|
145 super.onCancel(dialog); |
|
146 listener.onTextInputDialogCancelled(dialogId); |
|
147 } |
148 } |
148 } |