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