1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

Fix Android LineEdit editing bugs

This commit is contained in:
SkyJJ
2020-04-28 19:57:45 +02:00
parent 2fa795dea9
commit cc473b948f
14 changed files with 62 additions and 29 deletions

View File

@@ -460,9 +460,9 @@ public class GodotIO {
return (int)(metrics.density * 160f);
}
public void showKeyboard(String p_existing_text, int p_max_input_length) {
public void showKeyboard(String p_existing_text, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
if (edit != null)
edit.showKeyboard(p_existing_text, p_max_input_length);
edit.showKeyboard(p_existing_text, p_max_input_length, p_cursor_start, p_cursor_end);
//InputMethodManager inputMgr = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
//inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

View File

@@ -58,6 +58,7 @@ public class GodotEditText extends EditText {
private GodotTextInputWrapper mInputWrapper;
private EditHandler sHandler = new EditHandler(this);
private String mOriginText;
private int mMaxInputLength;
private static class EditHandler extends Handler {
private final WeakReference<GodotEditText> mEdit;
@@ -104,11 +105,18 @@ public class GodotEditText extends EditText {
String text = edit.mOriginText;
if (edit.requestFocus()) {
edit.removeTextChangedListener(edit.mInputWrapper);
setMaxInputLength(edit);
edit.setText("");
edit.append(text);
if (msg.arg2 != -1) {
edit.setSelection(msg.arg1, msg.arg2);
edit.mInputWrapper.setSelection(true);
} else {
edit.mInputWrapper.setSelection(false);
}
edit.mInputWrapper.setOriginText(text);
edit.addTextChangedListener(edit.mInputWrapper);
setMaxInputLength(edit, msg.arg1);
final InputMethodManager imm = (InputMethodManager)mRenderView.getView().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, 0);
}
@@ -125,14 +133,10 @@ public class GodotEditText extends EditText {
}
}
private void setMaxInputLength(EditText p_edit_text, int p_max_input_length) {
if (p_max_input_length > 0) {
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(p_max_input_length);
p_edit_text.setFilters(filters);
} else {
p_edit_text.setFilters(new InputFilter[] {});
}
private void setMaxInputLength(EditText p_edit_text) {
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(this.mMaxInputLength);
p_edit_text.setFilters(filters);
}
// ===========================================================
@@ -164,13 +168,24 @@ public class GodotEditText extends EditText {
// ===========================================================
// Methods
// ===========================================================
public void showKeyboard(String p_existing_text, int p_max_input_length) {
mOriginText = p_existing_text;
public void showKeyboard(String p_existing_text, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
int maxInputLength = (p_max_input_length <= 0) ? Integer.MAX_VALUE : p_max_input_length;
if (p_cursor_start == -1) { // cursor position not given
this.mOriginText = p_existing_text;
this.mMaxInputLength = maxInputLength;
} else if (p_cursor_end == -1) { // not text selection
this.mOriginText = p_existing_text.substring(0, p_cursor_start);
this.mMaxInputLength = maxInputLength - (p_existing_text.length() - p_cursor_start);
} else {
this.mOriginText = p_existing_text.substring(0, p_cursor_end);
this.mMaxInputLength = maxInputLength - (p_existing_text.length() - p_cursor_end);
}
final Message msg = new Message();
msg.what = HANDLER_OPEN_IME_KEYBOARD;
msg.obj = this;
msg.arg1 = p_max_input_length;
msg.arg1 = p_cursor_start;
msg.arg2 = p_cursor_end;
sHandler.sendMessage(msg);
}

View File

@@ -53,6 +53,7 @@ public class GodotTextInputWrapper implements TextWatcher, OnEditorActionListene
private final GodotRenderView mRenderView;
private final GodotEditText mEdit;
private String mOriginText;
private boolean mHasSelection;
// ===========================================================
// Constructors
@@ -77,6 +78,10 @@ public class GodotTextInputWrapper implements TextWatcher, OnEditorActionListene
mOriginText = originText;
}
public void setSelection(boolean selection) {
mHasSelection = selection;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@@ -95,6 +100,11 @@ public class GodotTextInputWrapper implements TextWatcher, OnEditorActionListene
for (int i = 0; i < count; ++i) {
GodotLib.key(KeyEvent.KEYCODE_DEL, KeyEvent.KEYCODE_DEL, 0, true);
GodotLib.key(KeyEvent.KEYCODE_DEL, KeyEvent.KEYCODE_DEL, 0, false);
if (mHasSelection) {
mHasSelection = false;
break;
}
}
}
});