author | Wuzzy <Wuzzy2@mail.ru> |
Thu, 15 Feb 2018 18:32:28 +0100 (2018-02-15) | |
changeset 13009 | 67cc3cac4c8e |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
1 |
/* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
4 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
9 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
13 |
* GNU General Public License for more details. |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
14 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
18 |
*/ |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
19 |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7461
diff
changeset
|
20 |
package org.hedgewars.hedgeroid.util; |
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
|
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
import android.app.Activity; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
import android.app.AlertDialog; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
import android.app.Dialog; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
import android.content.DialogInterface; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
import android.os.Bundle; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
import android.support.v4.app.DialogFragment; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
import android.view.KeyEvent; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
import android.view.inputmethod.EditorInfo; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
import android.widget.EditText; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
import android.widget.TextView; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
import android.widget.TextView.OnEditorActionListener; |
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 |
/** |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
* 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
|
36 |
* 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
|
37 |
*/ |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
public class TextInputDialog extends DialogFragment { |
10017 | 39 |
private static final String BUNDLE_DIALOG_ID = "dialogId"; |
40 |
private static final String BUNDLE_TITLE_TEXT = "title"; |
|
41 |
private static final String BUNDLE_MESSAGE_TEXT = "message"; |
|
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 |
} |
|
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
92 |
|
10017 | 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 |
}); |
|
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
116 |
|
10017 | 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 |
} |
|
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
148 |
} |