Skip to content

Commit 16ae112

Browse files
committed
Text Finished editing callback on android text field
1 parent 3df59dc commit 16ae112

File tree

4 files changed

+51
-44
lines changed

4 files changed

+51
-44
lines changed

cocos2d-ui/Platform/Android/CCPlatformTextFieldAndroid.m

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,6 @@
1414
#import "CCDirector.h"
1515
#import "CCEditText.h"
1616

17-
extern void objc_retain(id);
18-
19-
20-
typedef void (^CCPlatformTextFieldAndroidTextViewOnEditorActionListenerCompletionBlock)(void);
21-
22-
23-
BRIDGE_CLASS("org.cocos2d.CCPlatformTextFieldAndroidTextViewOnEditorActionListener")
24-
@interface CCPlatformTextFieldAndroidTextViewOnEditorActionListener : JavaObject<AndroidTextViewOnEditorActionListener>
25-
- (BOOL)onEditorAction:(AndroidTextView *)v actionId:(int32_t)actionId keyEvent:(AndroidKeyEvent *)event;
26-
27-
@end
28-
@implementation CCPlatformTextFieldAndroidTextViewOnEditorActionListener {
29-
CCPlatformTextFieldAndroidTextViewOnEditorActionListenerCompletionBlock _completionBlock;
30-
}
31-
32-
- (id) initWithCompletionBlock:(CCPlatformTextFieldAndroidTextViewOnEditorActionListenerCompletionBlock)completionBlock {
33-
if (self = [super init]) {
34-
_completionBlock = [completionBlock copy];
35-
}
36-
return self;
37-
38-
}
39-
40-
@bridge(callback) onEditorAction:actionId:keyEvent: = onEditorAction;
41-
- (BOOL)onEditorAction:(AndroidTextView *)v actionId:(int32_t)actionId keyEvent:(AndroidKeyEvent *)event {
42-
if (actionId == AndroidEditorInfoIME_ACTION_SEARCH ||
43-
actionId == AndroidEditorInfoIME_ACTION_DONE
44-
// || [event action] == KeyEvent.ACTION_DOWN && [event keyCode] == ANDROID_KEYCODE_ENTER
45-
) {
46-
_completionBlock();
47-
} else {
48-
NSLog(@"nothing");
49-
}
50-
return NO;
51-
}
52-
53-
@end
5417

5518
@implementation CCPlatformTextFieldAndroid {
5619
CCEditText *_editText;
@@ -68,6 +31,11 @@ - (id) init {
6831
return self;
6932
}
7033

34+
- (void)dealloc
35+
{
36+
_editText = nil;
37+
}
38+
7139

7240
- (void)onEnterTransitionDidFinish {
7341
[super onEnterTransitionDidFinish];
@@ -106,13 +74,16 @@ - (void) positionInControl:(CCControl *)control padding:(float)padding {
10674
AndroidRelativeLayoutLayoutParams *params = [[AndroidRelativeLayoutLayoutParams alloc] initWithWidth:frame.size.width height:frame.size.height];
10775
[params setMargins:frame.origin.x top:frame.origin.y right:0 bottom:0];
10876
[_editText setLayoutParams:params];
77+
[_editText setImeOptions:AndroidEditorInfoIME_FLAG_NO_EXTRACT_UI];
78+
10979

110-
[_editText setOnEditorActionListener:[[CCPlatformTextFieldAndroidTextViewOnEditorActionListener alloc] initWithCompletionBlock:^{
111-
if ([[self delegate] respondsToSelector:@selector(platformTextFieldDidFinishEditing:)]) {
112-
[[self delegate] platformTextFieldDidFinishEditing:self];
80+
__weak id weakSelf = self;
81+
[_editText setCompletionBlock:^{
82+
if ([[weakSelf delegate] respondsToSelector:@selector(platformTextFieldDidFinishEditing:)]) {
83+
[[weakSelf delegate] platformTextFieldDidFinishEditing:weakSelf];
11384
}
11485

115-
}]];
86+
}];
11687
});
11788
}
11889

@@ -153,4 +124,6 @@ - (void)setString:(NSString *)string {
153124
- (NSString *)string {
154125
return _editText.text;
155126
}
127+
128+
156129
@end

cocos2d-ui/Platform/Android/java/CCEditText.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
//
88

99
#import <BridgeKitV3/BridgeKit.h>
10+
11+
12+
typedef void (^CCEditTextCompletionBlock)(void);
13+
1014
BRIDGE_CLASS("org.cocos2d.CCEditText")
1115
@interface CCEditText : AndroidEditText
1216
- (id)initWithContext:(AndroidContext *)context;
13-
17+
- (BOOL)onKeyPreIme:(int32_t)keyCode keyEvent:(AndroidKeyEvent *)event;
1418
- (void)setTextSizeDouble:(double)textSize;
19+
- (void) setCompletionBlock:(CCEditTextCompletionBlock)completionBlock;
1520
@end

cocos2d-ui/Platform/Android/java/CCEditText.m

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,33 @@
88

99
#import "CCEditText.h"
1010

11-
@implementation CCEditText
11+
@implementation CCEditText {
12+
CCEditTextCompletionBlock _completionBlock;
13+
}
14+
1215
@bridge (constructor) initWithContext:;
1316
@bridge (method, instance) setTextSizeDouble:=setTextSizeDouble;
17+
18+
@bridge (callback) onKeyPreIme:keyEvent: = onKeyPreIme;
19+
- (BOOL)onKeyPreIme:(int32_t)keyCode keyEvent:(AndroidKeyEvent *)event {
20+
if (keyCode == AndroidKeyEventKeycodeBack || [event action] == AndroidKeyEventActionUp) {
21+
if (_completionBlock != nil) {
22+
_completionBlock();
23+
}
24+
25+
return NO;
26+
}
27+
return [self dispatchKeyEvent:event];
28+
29+
}
30+
31+
32+
- (void) setCompletionBlock:(CCEditTextCompletionBlock)completionBlock {
33+
_completionBlock = [completionBlock copy];
34+
}
35+
36+
- (void)dealloc {
37+
[_completionBlock release];
38+
[super dealloc];
39+
}
1440
@end

cocos2d-ui/Platform/Android/java/org/cocos2d/CCEditText.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import android.widget.EditText;
44
import android.util.Log;
55
import android.content.Context;
6+
import android.view.KeyEvent;
67

78
public class CCEditText extends EditText {
89
public CCEditText (Context context) {
910
super(context);
1011
}
1112

1213
public void setTextSizeDouble(double textSize) {
13-
Log.d("TEXTFIELD", "setTextSizeDouble "+textSize);
1414
setTextSize((float)textSize);
1515
}
16+
17+
public native boolean onKeyPreIme(int keyCode, KeyEvent event);
18+
1619
}

0 commit comments

Comments
 (0)