From fc6976cd133a01ff5495e8bfb169ac220e2e5e70 Mon Sep 17 00:00:00 2001 From: iPel Date: Thu, 16 Nov 2023 15:57:33 +0800 Subject: [PATCH] feat(android,doc): `returnKeyType`,`keyboardType` not change singleLine (#3601) * feat(android,doc): `returnKeyType`,`keyboardType` not change singleLine * refactor(android): remove unnecessary comment --------- Co-authored-by: siguangli --- docs/api/hippy-react/components.md | 2 +- docs/api/hippy-vue/components.md | 2 +- .../hippy/views/textinput/HippyTextInput.java | 20 ++++++++++++++++--- .../textinput/HippyTextInputController.java | 15 ++++++++------ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/docs/api/hippy-react/components.md b/docs/api/hippy-react/components.md index 80cc3a9fa49..33567fe4a45 100644 --- a/docs/api/hippy-react/components.md +++ b/docs/api/hippy-react/components.md @@ -337,7 +337,7 @@ import icon from './qb_icon_new.png'; | onSelectionChange | 当输入框选择文字的范围被改变时调用。返回参数的样式如 `nativeEvent: { selection: { start, end } }`。 | `Function` | `Android、iOS、Web-Renderer、Voltron` | | placeholder | 如果没有任何文字输入,会显示此字符串。 | `string` | `Android、iOS、hippy-react-web、Web-Renderer、Voltron` | | placeholderTextColor | 占位字符串显示的文字颜色。(也可设置为 Style 属性)`最低支持版本2.13.4` | [`color`](api/style/color.md) | `Android、iOS、Web-Renderer、Voltron` | -| returnKeyType | 指定软键盘的回车键显示的样式。 | `enum (done, go, next, search, send)` | `Android、iOS、Web-Renderer、Voltron` | +| returnKeyType | 指定软键盘的回车键显示的样式。(其中部分样式仅`multiline=false`时有效) | `enum (done, go, next, search, send)` | `Android、iOS、Web-Renderer、Voltron` | | underlineColorAndroid | `TextInput` 下底线的颜色。 可以设置为 'transparent' 来去掉下底线。(也可设置为 Style 属性) | [`color`](api/style/color.md) | `Android` | | value | 指定 `TextInput` 组件的值。 | `string` | `Android、iOS、hippy-react-web、Web-Renderer、Voltron` | | autoFocus | 组件渲染时自动获得焦点。 | `boolean` | `Android、iOS、hippy-react-web、Web-Renderer、Voltron` | diff --git a/docs/api/hippy-vue/components.md b/docs/api/hippy-vue/components.md index f672300e187..adb283c888c 100644 --- a/docs/api/hippy-vue/components.md +++ b/docs/api/hippy-vue/components.md @@ -258,7 +258,7 @@ | placeholder | 如果没有任何文字输入,会显示此字符串。 | `string` | `Android、iOS、Web-Renderer、Voltron` | | placeholder-text-color | 占位字符串显示的文字颜色。(也可设置为 Style 属性) `最低支持版本2.13.4` | [`color`](api/style/color.md) | `Android、iOS、Web-Renderer、Voltron` | | underline-color-android | `input` 下底线的颜色。 可以设置为 'transparent' 来去掉下底线。(也可设置为 Style 属性) `最低支持版本2.13.4` | [`color`](api/style/color.md) | `Android` | -| returnKeyType | 指定软键盘的回车键显示的样式。 | `enum(done, go, next, search, send)` | `Android、iOS、Web-Renderer` | +| returnKeyType | 指定软键盘的回车键显示的样式。(其中部分样式仅对单行文本组件有效) | `enum(done, go, next, search, send)` | `Android、iOS、Web-Renderer` | | value | 指定 `input` 组件的值。 | `string` | `Android、iOS、Web-Renderer、Voltron` | | break-strategy* | 设置Android API 23及以上系统的文本换行策略。`default: simple` | `enum(simple, high_quality, balanced)` | `Android(版本 2.14.2以上)` | diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/textinput/HippyTextInput.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/textinput/HippyTextInput.java index b08b4beca89..b114f0c8521 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/textinput/HippyTextInput.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/textinput/HippyTextInput.java @@ -62,6 +62,7 @@ public class HippyTextInput extends AppCompatEditText implements HippyViewBase, TextView.OnEditorActionListener, View.OnFocusChangeListener { + private static final String TAG = "HippyTextInput"; boolean mHasAddWatcher = false; private String mPreviousText = ""; TextWatcher mTextWatcher = null; @@ -272,7 +273,7 @@ private int getScreenHeight() { } } catch (SecurityException e) { - LogUtils.d("HippyTextInput", "getScreenHeight: " + e.getMessage()); + LogUtils.d(TAG, "getScreenHeight: " + e.getMessage()); } return -1; } @@ -616,12 +617,25 @@ public void setCursorColor(int color) { } break; } catch (Throwable e) { - LogUtils.d("HippyTextInput", "setCursorColor: " + e.getMessage()); + LogUtils.d(TAG, "setCursorColor: " + e.getMessage()); } editorClass = editorClass.getSuperclass(); //继续往上反射父亲 } } catch (Throwable e) { - LogUtils.d("HippyTextInput", "setCursorColor: " + e.getMessage()); + LogUtils.d(TAG, "setCursorColor: " + e.getMessage()); + } + } + } + + public void refreshSoftInput() { + InputMethodManager imm = getInputMethodManager(); + if (imm.isActive(this)) { // refresh the showing soft keyboard + try { + imm.hideSoftInputFromWindow(getWindowToken(), 0); + imm.restartInput(this); + imm.showSoftInput(this, 0, null); + } catch (Exception e) { + LogUtils.e(TAG, "refreshSoftInput error", e); } } } diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/textinput/HippyTextInputController.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/textinput/HippyTextInputController.java index 9a750cd7cf3..73db6e0fc0e 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/textinput/HippyTextInputController.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/views/textinput/HippyTextInputController.java @@ -35,6 +35,7 @@ import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; +import android.view.inputmethod.InputMethodManager; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -162,30 +163,26 @@ public void setReturnKeyType(HippyTextInput view, String returnKeyType) { break; case "next": returnKeyFlag = EditorInfo.IME_ACTION_NEXT; - view.setSingleLine(true); break; case "none": returnKeyFlag = EditorInfo.IME_ACTION_NONE; break; case "previous": returnKeyFlag = EditorInfo.IME_ACTION_PREVIOUS; - view.setSingleLine(true); break; case "search": returnKeyFlag = EditorInfo.IME_ACTION_SEARCH; - view.setSingleLine(true); break; case "send": returnKeyFlag = EditorInfo.IME_ACTION_SEND; - view.setSingleLine(true); break; case "done": returnKeyFlag = EditorInfo.IME_ACTION_DONE; - view.setSingleLine(true); break; } } view.setImeOptions(returnKeyFlag | EditorInfo.IME_FLAG_NO_FULLSCREEN); + view.refreshSoftInput(); } @HippyControllerProps(name = "keyboardType", defaultType = HippyControllerProps.STRING) @@ -201,8 +198,14 @@ public void setKeyboardType(HippyTextInput hippyTextInput, String keyboardType) flagsToSet = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD; hippyTextInput.setTransformationMethod(PasswordTransformationMethod.getInstance()); } - + boolean multiline = (hippyTextInput.getInputType() & InputType.TYPE_TEXT_FLAG_MULTI_LINE) != 0; + if (multiline) { + flagsToSet |= InputType.TYPE_TEXT_FLAG_MULTI_LINE; + } else { + flagsToSet &= ~InputType.TYPE_TEXT_FLAG_MULTI_LINE; + } hippyTextInput.setInputType(flagsToSet); + hippyTextInput.refreshSoftInput(); } private static int parseFontWeight(String fontWeightString) {