Skip to content

Commit

Permalink
Merge pull request #86619 from Alex2782/fix_flag_decimal
Browse files Browse the repository at this point in the history
Fix virtual keyboard for decimal values on Android
  • Loading branch information
akien-mga committed Feb 13, 2024
2 parents 7b30203 + 6f91c00 commit 68c583d
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.DigitsKeyListener;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.lang.ref.WeakReference;
import java.util.Locale;

public class GodotEditText extends EditText {
// ===========================================================
Expand Down Expand Up @@ -137,6 +141,7 @@ private void handleMessage(final Message msg) {
}

int inputType = InputType.TYPE_CLASS_TEXT;
String acceptCharacters = null;
switch (edit.getKeyboardType()) {
case KEYBOARD_TYPE_DEFAULT:
inputType = InputType.TYPE_CLASS_TEXT;
Expand All @@ -148,7 +153,8 @@ private void handleMessage(final Message msg) {
inputType = InputType.TYPE_CLASS_NUMBER;
break;
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;
case KEYBOARD_TYPE_PHONE:
inputType = InputType.TYPE_CLASS_PHONE;
Expand All @@ -165,6 +171,14 @@ private void handleMessage(final Message msg) {
}
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.addTextChangedListener(edit.mInputWrapper);
final InputMethodManager imm = (InputMethodManager)mRenderView.getView().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Expand Down

0 comments on commit 68c583d

Please sign in to comment.