Merge pull request #35438 from MadEqua/virtual-keyboard-line-edit

Android virtual keyboard respecting LineEdit max length.
This commit is contained in:
Rémi Verschelde 2020-01-23 08:20:24 +01:00 committed by GitHub
commit c2e07db071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 31 additions and 18 deletions

View File

@ -221,7 +221,7 @@ bool OS::has_virtual_keyboard() const {
return false; return false;
} }
void OS::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) { void OS::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect, int p_max_input_length) {
} }
void OS::hide_virtual_keyboard() { void OS::hide_virtual_keyboard() {

View File

@ -380,7 +380,7 @@ public:
}; };
virtual bool has_virtual_keyboard() const; virtual bool has_virtual_keyboard() const;
virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2()); virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), int p_max_input_length = -1);
virtual void hide_virtual_keyboard(); virtual void hide_virtual_keyboard();
// returns height of the currently shown virtual keyboard (0 if keyboard is hidden) // returns height of the currently shown virtual keyboard (0 if keyboard is hidden)

View File

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

View File

@ -32,6 +32,7 @@ package org.godotengine.godot.input;
import android.content.Context; import android.content.Context;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.text.InputFilter;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
@ -104,6 +105,7 @@ public class GodotEditText extends EditText {
edit.append(text); edit.append(text);
edit.mInputWrapper.setOriginText(text); edit.mInputWrapper.setOriginText(text);
edit.addTextChangedListener(edit.mInputWrapper); edit.addTextChangedListener(edit.mInputWrapper);
setMaxInputLength(edit, msg.arg1);
final InputMethodManager imm = (InputMethodManager)mView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); final InputMethodManager imm = (InputMethodManager)mView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, 0); imm.showSoftInput(edit, 0);
} }
@ -120,6 +122,16 @@ 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[] {});
}
}
// =========================================================== // ===========================================================
// Getter & Setter // Getter & Setter
// =========================================================== // ===========================================================
@ -149,12 +161,13 @@ public class GodotEditText extends EditText {
// =========================================================== // ===========================================================
// Methods // Methods
// =========================================================== // ===========================================================
public void showKeyboard(String p_existing_text) { public void showKeyboard(String p_existing_text, int p_max_input_length) {
this.mOriginText = p_existing_text; this.mOriginText = p_existing_text;
final Message msg = new Message(); final Message msg = new Message();
msg.what = HANDLER_OPEN_IME_KEYBOARD; msg.what = HANDLER_OPEN_IME_KEYBOARD;
msg.obj = this; msg.obj = this;
msg.arg1 = p_max_input_length;
sHandler.sendMessage(msg); sHandler.sendMessage(msg);
} }

View File

@ -53,7 +53,7 @@ GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instanc
_get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;"); _get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;");
_get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I"); _get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I");
_get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;"); _get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;");
_show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;)V"); _show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;I)V");
_hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V"); _hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V");
_set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V"); _set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V");
_get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(I)Ljava/lang/String;"); _get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(I)Ljava/lang/String;");
@ -135,11 +135,11 @@ bool GodotIOJavaWrapper::has_vk() {
return (_show_keyboard != 0) && (_hide_keyboard != 0); return (_show_keyboard != 0) && (_hide_keyboard != 0);
} }
void GodotIOJavaWrapper::show_vk(const String &p_existing) { void GodotIOJavaWrapper::show_vk(const String &p_existing, int p_max_input_length) {
if (_show_keyboard) { if (_show_keyboard) {
JNIEnv *env = ThreadAndroid::get_env(); JNIEnv *env = ThreadAndroid::get_env();
jstring jStr = env->NewStringUTF(p_existing.utf8().get_data()); jstring jStr = env->NewStringUTF(p_existing.utf8().get_data());
env->CallVoidMethod(godot_io_instance, _show_keyboard, jStr); env->CallVoidMethod(godot_io_instance, _show_keyboard, jStr, p_max_input_length);
} }
} }

View File

@ -73,7 +73,7 @@ public:
int get_screen_dpi(); int get_screen_dpi();
String get_unique_id(); String get_unique_id();
bool has_vk(); bool has_vk();
void show_vk(const String &p_existing); void show_vk(const String &p_existing, int p_max_input_length);
void hide_vk(); void hide_vk();
int get_vk_height(); int get_vk_height();
void set_vk_height(int p_height); void set_vk_height(int p_height);

View File

@ -559,10 +559,10 @@ int OS_Android::get_virtual_keyboard_height() const {
// return 0; // return 0;
} }
void OS_Android::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) { void OS_Android::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect, int p_max_input_length) {
if (godot_io_java->has_vk()) { if (godot_io_java->has_vk()) {
godot_io_java->show_vk(p_existing_text); godot_io_java->show_vk(p_existing_text, p_max_input_length);
} else { } else {
ERR_PRINT("Virtual keyboard not available"); ERR_PRINT("Virtual keyboard not available");

View File

@ -158,7 +158,7 @@ public:
virtual bool has_touchscreen_ui_hint() const; virtual bool has_touchscreen_ui_hint() const;
virtual bool has_virtual_keyboard() const; virtual bool has_virtual_keyboard() const;
virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2()); virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), int p_max_input_length = -1);
virtual void hide_virtual_keyboard(); virtual void hide_virtual_keyboard();
virtual int get_virtual_keyboard_height() const; virtual int get_virtual_keyboard_height() const;

View File

@ -482,7 +482,7 @@ extern Error _shell_open(String p_uri);
extern void _set_keep_screen_on(bool p_enabled); extern void _set_keep_screen_on(bool p_enabled);
extern void _vibrate(); extern void _vibrate();
void OSIPhone::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) { void OSIPhone::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect, int p_max_input_length) {
_show_keyboard(p_existing_text); _show_keyboard(p_existing_text);
}; };

View File

@ -165,7 +165,7 @@ public:
virtual bool can_draw() const; virtual bool can_draw() const;
virtual bool has_virtual_keyboard() const; virtual bool has_virtual_keyboard() const;
virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2()); virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), int p_max_input_length = -1);
virtual void hide_virtual_keyboard(); virtual void hide_virtual_keyboard();
virtual int get_virtual_keyboard_height() const; virtual int get_virtual_keyboard_height() const;

View File

@ -801,7 +801,7 @@ bool OS_UWP::has_virtual_keyboard() const {
return UIViewSettings::GetForCurrentView()->UserInteractionMode == UserInteractionMode::Touch; return UIViewSettings::GetForCurrentView()->UserInteractionMode == UserInteractionMode::Touch;
} }
void OS_UWP::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) { void OS_UWP::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect, int p_max_input_length) {
InputPane ^ pane = InputPane::GetForCurrentView(); InputPane ^ pane = InputPane::GetForCurrentView();
pane->TryShow(); pane->TryShow();

View File

@ -239,7 +239,7 @@ public:
virtual bool has_touchscreen_ui_hint() const; virtual bool has_touchscreen_ui_hint() const;
virtual bool has_virtual_keyboard() const; virtual bool has_virtual_keyboard() const;
virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2()); virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), int p_max_input_length = -1);
virtual void hide_virtual_keyboard(); virtual void hide_virtual_keyboard();
virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false); virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false);

View File

@ -128,7 +128,7 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) {
selection.doubleclick = false; selection.doubleclick = false;
if (OS::get_singleton()->has_virtual_keyboard()) if (OS::get_singleton()->has_virtual_keyboard())
OS::get_singleton()->show_virtual_keyboard(text, get_global_rect()); OS::get_singleton()->show_virtual_keyboard(text, get_global_rect(), max_length);
} }
update(); update();
@ -913,7 +913,7 @@ void LineEdit::_notification(int p_what) {
OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos); OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos);
if (OS::get_singleton()->has_virtual_keyboard()) if (OS::get_singleton()->has_virtual_keyboard())
OS::get_singleton()->show_virtual_keyboard(text, get_global_rect()); OS::get_singleton()->show_virtual_keyboard(text, get_global_rect(), max_length);
} break; } break;
case NOTIFICATION_FOCUS_EXIT: { case NOTIFICATION_FOCUS_EXIT: {