Merge pull request #86619 from Alex2782/fix_flag_decimal

Fix virtual keyboard for decimal values on Android
This commit is contained in:
Rémi Verschelde 2024-02-13 23:43:11 +01:00
commit 68c583d30c
No known key found for this signature in database
GPG key ID: C3336907360768E1

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);