Fix virtual keyboard for decimal values on Android

(cherry picked from commit 6f91c00056)
This commit is contained in:
Alexander Hartmann 2023-12-29 23:16:03 +01:00 committed by Rémi Verschelde
parent c8ae076c1a
commit 601afdf1d5
No known key found for this signature in database
GPG Key ID: C3336907360768E1
1 changed files with 15 additions and 1 deletions

View File

@ -34,10 +34,13 @@ import org.godotengine.godot.*;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.text.InputFilter; import android.text.InputFilter;
import android.text.InputType; import android.text.InputType;
import android.text.TextUtils;
import android.text.method.DigitsKeyListener;
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;
@ -45,6 +48,7 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.EditText; import android.widget.EditText;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.Locale;
public class GodotEditText extends EditText { public class GodotEditText extends EditText {
// =========================================================== // ===========================================================
@ -137,6 +141,7 @@ public class GodotEditText extends EditText {
} }
int inputType = InputType.TYPE_CLASS_TEXT; int inputType = InputType.TYPE_CLASS_TEXT;
String acceptCharacters = null;
switch (edit.getKeyboardType()) { switch (edit.getKeyboardType()) {
case KEYBOARD_TYPE_DEFAULT: case KEYBOARD_TYPE_DEFAULT:
inputType = InputType.TYPE_CLASS_TEXT; inputType = InputType.TYPE_CLASS_TEXT;
@ -148,7 +153,8 @@ public class GodotEditText extends EditText {
inputType = InputType.TYPE_CLASS_NUMBER; inputType = InputType.TYPE_CLASS_NUMBER;
break; break;
case KEYBOARD_TYPE_NUMBER_DECIMAL: case KEYBOARD_TYPE_NUMBER_DECIMAL:
inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED; inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL;
acceptCharacters = "0123456789,.- ";
break; break;
case KEYBOARD_TYPE_PHONE: case KEYBOARD_TYPE_PHONE:
inputType = InputType.TYPE_CLASS_PHONE; inputType = InputType.TYPE_CLASS_PHONE;
@ -165,6 +171,14 @@ public class GodotEditText extends EditText {
} }
edit.setInputType(inputType); edit.setInputType(inputType);
if (!TextUtils.isEmpty(acceptCharacters)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
edit.setKeyListener(DigitsKeyListener.getInstance(Locale.getDefault()));
} else {
edit.setKeyListener(DigitsKeyListener.getInstance(acceptCharacters));
}
}
edit.mInputWrapper.setOriginText(text); edit.mInputWrapper.setOriginText(text);
edit.addTextChangedListener(edit.mInputWrapper); edit.addTextChangedListener(edit.mInputWrapper);
final InputMethodManager imm = (InputMethodManager)mRenderView.getView().getContext().getSystemService(Context.INPUT_METHOD_SERVICE); final InputMethodManager imm = (InputMethodManager)mRenderView.getView().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);