Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathCocos2dxEditBoxDialog.java
More file actions
412 lines (342 loc) · 15.6 KB
/
Copy pathCocos2dxEditBoxDialog.java
File metadata and controls
412 lines (342 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package com.example.crossgate;
import com.unity3d.player.UnityPlayer;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputFilter;
import android.text.InputType;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
public class Cocos2dxEditBoxDialog extends Dialog {
// ===========================================================
// Constants
// ===========================================================
/**
* The user is allowed to enter any text, including line breaks.
*/
private final int kEditBoxInputModeAny = 0;
/**
* The user is allowed to enter an e-mail address.
*/
private final int kEditBoxInputModeEmailAddr = 1;
/**
* The user is allowed to enter an integer value.
*/
private final int kEditBoxInputModeNumeric = 2;
/**
* The user is allowed to enter a phone number.
*/
private final int kEditBoxInputModePhoneNumber = 3;
/**
* The user is allowed to enter a URL.
*/
private final int kEditBoxInputModeUrl = 4;
/**
* The user is allowed to enter a real number value. This extends kEditBoxInputModeNumeric by allowing a decimal point.
*/
private final int kEditBoxInputModeDecimal = 5;
/**
* The user is allowed to enter any text, except for line breaks.
*/
private final int kEditBoxInputModeSingleLine = 6;
/**
* Indicates that the text entered is confidential data that should be obscured whenever possible. This implies EDIT_BOX_INPUT_FLAG_SENSITIVE.
*/
private final int kEditBoxInputFlagPassword = 0;
private final int kEditBoxInputFlagNormal = -1;
/**
* Indicates that the text entered is sensitive data that the implementation must never store into a dictionary or table for use in predictive, auto-completing, or other accelerated input schemes. A credit card number is an example of sensitive data.
*/
private final int kEditBoxInputFlagSensitive = 1;
/**
* This flag is a hint to the implementation that during text editing, the initial letter of each word should be capitalized.
*/
private final int kEditBoxInputFlagInitialCapsWord = 2;
/**
* This flag is a hint to the implementation that during text editing, the initial letter of each sentence should be capitalized.
*/
private final int kEditBoxInputFlagInitialCapsSentence = 3;
/**
* Capitalize all characters automatically.
*/
private final int kEditBoxInputFlagInitialCapsAllCharacters = 4;
private final int kKeyboardReturnTypeDefault = 0;
private final int kKeyboardReturnTypeDone = 1;
private final int kKeyboardReturnTypeSend = 2;
private final int kKeyboardReturnTypeSearch = 3;
private final int kKeyboardReturnTypeGo = 4;
// ===========================================================
// Fields
// ===========================================================
private EditText mInputEditText;
private TextView mTextViewTitle;
private Button mButtonOK;
private final String mTitle;
private final String mMessage;
private final int mInputMode;
private final int mInputFlag;
private final int mReturnType;
private final int mMaxLength;
private int mInputFlagConstraints;
private int mInputModeContraints;
private boolean mIsMultiline;
// ===========================================================
// Constructors
// ===========================================================
public Cocos2dxEditBoxDialog(final Context pContext, final String pTitle, final String pMessage, final int pInputMode, final int pInputFlag, final int pReturnType, final int pMaxLength) {
super(pContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
// super(context, R.style.Theme_Translucent);
this.mTitle = pTitle;
this.mMessage = pMessage;
this.mInputMode = pInputMode;
this.mInputFlag = pInputFlag;
this.mReturnType = pReturnType;
this.mMaxLength = pMaxLength;
}
private String mGameObjName;
private String mMethodName;
public void InitUnityMethod(String gameObjName, String methodName)
{
mGameObjName = gameObjName;
mMethodName = methodName;
}
@Override
protected void onCreate(final Bundle pSavedInstanceState) {
try
{
super.onCreate(pSavedInstanceState);
this.getWindow().setBackgroundDrawable(new ColorDrawable(0x80000000));
final LinearLayout layout = new LinearLayout(this.getContext());
layout.setOrientation(LinearLayout.VERTICAL);
//添加以下代码
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SendToUnityImeString();
Cocos2dxEditBoxDialog.this.closeKeyboard();
Cocos2dxEditBoxDialog.this.dismiss();
}
});
//=========
final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
this.mTextViewTitle = new TextView(this.getContext());
final LinearLayout.LayoutParams textviewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textviewParams.leftMargin = textviewParams.rightMargin = this.convertDipsToPixels(10);
this.mTextViewTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
layout.addView(this.mTextViewTitle, textviewParams);
//增加的布局
final LinearLayout childLayout = new LinearLayout(this.getContext());
childLayout.setOrientation(LinearLayout.HORIZONTAL);
final LinearLayout.LayoutParams childLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
this.mInputEditText = new EditText(this.getContext());
final LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editTextParams.leftMargin = editTextParams.rightMargin = this.convertDipsToPixels(10);
editTextParams.weight = 1;
//
childLayout.addView(this.mInputEditText,editTextParams);
//按钮
this.mButtonOK = new Button(this.getContext());
this.mButtonOK.setText("确定");
this.mButtonOK.setOnClickListener(new OnClickListener());
//
final LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
buttonParams.leftMargin = this.convertDipsToPixels(0);
buttonParams.rightMargin = this.convertDipsToPixels(10);
childLayout.addView(this.mButtonOK, buttonParams); // 加入到child layout
layout.addView(childLayout,childLayoutParams);
//layout.addView(this.mInputEditText, editTextParams);
this.setContentView(layout, layoutParams);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.mTextViewTitle.setText(this.mTitle);
this.mInputEditText.setText(this.mMessage);
int oldImeOptions = this.mInputEditText.getImeOptions();
this.mInputEditText.setImeOptions(oldImeOptions | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
oldImeOptions = this.mInputEditText.getImeOptions();
switch (this.mInputMode) {
case kEditBoxInputModeAny:
this.mInputModeContraints = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE;
break;
case kEditBoxInputModeEmailAddr:
this.mInputModeContraints = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
break;
case kEditBoxInputModeNumeric:
this.mInputModeContraints = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED;
break;
case kEditBoxInputModePhoneNumber:
this.mInputModeContraints = InputType.TYPE_CLASS_PHONE;
break;
case kEditBoxInputModeUrl:
this.mInputModeContraints = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI;
break;
case kEditBoxInputModeDecimal:
this.mInputModeContraints = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED;
break;
case kEditBoxInputModeSingleLine:
this.mInputModeContraints = InputType.TYPE_CLASS_TEXT;
break;
default:
break;
}
if (this.mIsMultiline) {
this.mInputModeContraints |= InputType.TYPE_TEXT_FLAG_MULTI_LINE;
}
this.mInputEditText.setInputType(this.mInputModeContraints | this.mInputFlagConstraints);
switch (this.mInputFlag) {
case kEditBoxInputFlagPassword:
this.mInputFlagConstraints = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
break;
case kEditBoxInputFlagSensitive:
this.mInputFlagConstraints = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
break;
case kEditBoxInputFlagInitialCapsWord:
this.mInputFlagConstraints = InputType.TYPE_TEXT_FLAG_CAP_WORDS;
break;
case kEditBoxInputFlagInitialCapsSentence:
this.mInputFlagConstraints = InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
break;
case kEditBoxInputFlagInitialCapsAllCharacters:
this.mInputFlagConstraints = InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
break;
default:
this.mInputFlagConstraints = 0;
break;
}
this.mInputEditText.setInputType(this.mInputFlagConstraints | this.mInputModeContraints);
switch (this.mReturnType) {
case kKeyboardReturnTypeDefault:
this.mInputEditText.setImeOptions(oldImeOptions | EditorInfo.IME_ACTION_NONE);
break;
case kKeyboardReturnTypeDone:
this.mInputEditText.setImeOptions(oldImeOptions | EditorInfo.IME_ACTION_DONE);
break;
case kKeyboardReturnTypeSend:
this.mInputEditText.setImeOptions(oldImeOptions | EditorInfo.IME_ACTION_SEND);
break;
case kKeyboardReturnTypeSearch:
this.mInputEditText.setImeOptions(oldImeOptions | EditorInfo.IME_ACTION_SEARCH);
break;
case kKeyboardReturnTypeGo:
this.mInputEditText.setImeOptions(oldImeOptions | EditorInfo.IME_ACTION_GO);
break;
default:
this.mInputEditText.setImeOptions(oldImeOptions | EditorInfo.IME_ACTION_NONE);
break;
}
if (this.mMaxLength > 0) {
this.mInputEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(this.mMaxLength) });
}
final Handler initHandler = new Handler();
initHandler.postDelayed(new Runnable() {
@Override
public void run() {
try
{
Cocos2dxEditBoxDialog.this.mInputEditText.requestFocus();
Cocos2dxEditBoxDialog.this.mInputEditText.setSelection(Cocos2dxEditBoxDialog.this.mInputEditText.length());
Cocos2dxEditBoxDialog.this.openKeyboard();
} catch (Exception e)
{
}
}
}, 200);
this.mInputEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
try
{
// If user didn't set keyboard type, this callback will be invoked twice with 'KeyEvent.ACTION_DOWN' and 'KeyEvent.ACTION_UP'.
if (actionId != EditorInfo.IME_NULL || (actionId == EditorInfo.IME_NULL && event != null && event.getAction() == KeyEvent.ACTION_DOWN)) {
// Cocos2dxHelper.setEditTextDialogResult(Cocos2dxEditBoxDialog.this.mInputEditText.getText().toString());
// 发送给UNITY IME接收
SendToUnityImeString();
Cocos2dxEditBoxDialog.this.closeKeyboard();
Cocos2dxEditBoxDialog.this.dismiss();
return true;
}
return false;
} catch (Exception e)
{
return false;
}
}
});
} catch (Exception e)
{
Log.i("Unity", "Create Cocos2dxEditorDialog Exception");
}
}
private void SendToUnityImeString()
{
SendToUnityImeString(Cocos2dxEditBoxDialog.this.mInputEditText.getText().toString());
}
// 发送给UNITY层
private void SendToUnityImeString(String value)
{
try
{
if (mGameObjName == null || mGameObjName.isEmpty() || mMethodName == null || mMethodName.isEmpty())
return;
UnityPlayer.UnitySendMessage(mGameObjName, mMethodName, value);
} catch (Exception e)
{
}
}
// 按钮点击事件
class OnClickListener implements android.view.View.OnClickListener {
@Override
public void onClick(View v) {
try
{
// 回调给Cocos2d并且关闭键盘
// Cocos2dxHelper.setEditTextDialogResult(Cocos2dxEditBoxDialog.this.mInputEditText.getText().toString());
SendToUnityImeString();
Cocos2dxEditBoxDialog.this.closeKeyboard();
Cocos2dxEditBoxDialog.this.dismiss();
} catch (Exception e)
{
}
}
}
private int convertDipsToPixels(final float pDIPs) {
try
{
final float scale = this.getContext().getResources().getDisplayMetrics().density;
return Math.round(pDIPs * scale);
} catch (Exception e)
{
return 0;
}
}
private void openKeyboard() {
try
{
final InputMethodManager imm = (InputMethodManager) this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(this.mInputEditText, 0);
} catch (Exception e)
{
}
}
private void closeKeyboard() {
try
{
final InputMethodManager imm = (InputMethodManager) this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.mInputEditText.getWindowToken(), 0);
} catch (Exception e)
{
}
}
}
You can’t perform that action at this time.
