IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> TvSetting添加隐藏WiFi中输入框无法跳转到下一步的问题 -> 正文阅读

[移动开发]TvSetting添加隐藏WiFi中输入框无法跳转到下一步的问题

? ? ? ? 在某个盒子项目开发中,有一项需求,使用系统原生的setting去验证隐藏ssid的连接功能,而这部分正常来说一般不会出现问题,但在这里,还是遇到了一个非常棘手的问题:输入ssid后按输入法的enter键后,退出了输入法,但并没有跳到下一步继续执行。

? ? ? ? 后排查发现,是因为我们换了输入,以前使用Android原生的输入法LatinIME时并无此问题,主要是因为LatinIME中有一个">"键,按这个键后就能跳到下一步去。

LatinIME中的ENTER键

? ? ? ? LatinIME中的ENTER键就是绿色那个">",通过在下面的位置加入打印后可以获取:

// android/packages/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/PointerTracker.java

    private void onDownEvent(final int x, final int y, final long eventTime,
            final KeyDetector keyDetector) {
        setKeyDetectorInner(keyDetector);
        if (DEBUG_EVENT) {
            printTouchEvent("onDownEvent:", x, y, eventTime);
        }

        final Key key = getKeyOn(x, y);
        Log.d("xxxx", "key down :" + key);
    ……

? ? ? ? 所有的LatinIME的按键都会调用这个方法,在这里将key打印出来,按下">"键时打印如下:

xxxx: key down :enter 1650,120 261x90

? ? ? ? 可以获知该键为ENTER键,而在代码中,其code为Constants.CODE_ENTER。

? ? ? ? 经过在LatinIME的源码中搜索,结合EditText的代码,可以猜测其触发TextView的IME_ACTION_NEXT,大概猜测关键代码在:

// android/packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
private void handleNonFunctionalEvent(final Event event,
        final InputTransaction inputTransaction,
        final LatinIME.UIHandler handler) {
    inputTransaction.setDidAffectContents();
    switch (event.mCodePoint) {
        case Constants.CODE_ENTER:
            final EditorInfo editorInfo = getCurrentInputEditorInfo();
            final int imeOptionsActionId =
                    InputTypeUtils.getImeOptionsActionIdFromEditorInfo(editorInfo);
            if (InputTypeUtils.IME_ACTION_CUSTOM_LABEL == imeOptionsActionId) {
                // Either we have an actionLabel and we should performEditorAction with
                // actionId regardless of its value.
                performEditorAction(editorInfo.actionId);
            } else if (EditorInfo.IME_ACTION_NONE != imeOptionsActionId) {
                // We didn't have an actionLabel, but we had another action to execute.
                // EditorInfo.IME_ACTION_NONE explicitly means no action. In contrast,
                // EditorInfo.IME_ACTION_UNSPECIFIED is the default value for an action, so it
                // means there should be an action and the app didn't bother to set a specific
                // code for it - presumably it only handles one. It does not have to be treated
                // in any specific way: anything that is not IME_ACTION_NONE should be sent to
                // performEditorAction.
                performEditorAction(imeOptionsActionId);
            } else {
                // No action label, and the action from imeOptions is NONE: this is a regular
                // enter key that should input a carriage return.
                handleNonSpecialCharacterEvent(event, inputTransaction, handler);
            }
            break;
        default:
            handleNonSpecialCharacterEvent(event, inputTransaction, handler);
            break;
    }
}

? ? ? ? ? ? ? 加上堆栈的打印后,获取的堆栈信息如下:

08-06 10:57:19.175  2428  2428 W System.err: java.lang.Exception: performEditorAction: 5
08-06 10:57:19.175  2428  2428 W System.err:    at com.android.inputmethod.latin.inputlogic.InputLogic.performEditorAction(InputLogic.java:1830)
08-06 10:57:19.175  2428  2428 W System.err:    at com.android.inputmethod.latin.inputlogic.InputLogic.handleNonFunctionalEvent(InputLogic.java:738)
08-06 10:57:19.175  2428  2428 W System.err:    at com.android.inputmethod.latin.inputlogic.InputLogic.onCodeInput(InputLogic.java:470)
08-06 10:57:19.175  2428  2428 W System.err:    at com.android.inputmethod.latin.LatinIME.onEvent(LatinIME.java:1678)
08-06 10:57:19.175  2428  2428 W System.err:    at com.android.inputmethod.latin.LatinIME.onCodeInput(LatinIME.java:1668)
08-06 10:57:19.176  2428  2428 W System.err:    at com.android.inputmethod.keyboard.PointerTracker.callListenerOnCodeInput(PointerTracker.java:298)
08-06 10:57:19.176  2428  2428 W System.err:    at com.android.inputmethod.keyboard.PointerTracker.detectAndSendKey(PointerTracker.java:1157)
08-06 10:57:19.176  2428  2428 W System.err:    at com.android.inputmethod.keyboard.PointerTracker.onUpEventInternal(PointerTracker.java:999)
08-06 10:57:19.176  2428  2428 W System.err:    at com.android.inputmethod.keyboard.PointerTracker.onUpEvent(PointerTracker.java:941)
08-06 10:57:19.176  2428  2428 W System.err:    at com.android.inputmethod.keyboard.PointerTracker.processMotionEvent(PointerTracker.java:608)
08-06 10:57:19.176  2428  2428 W System.err:    at com.android.inputmethod.keyboard.internal.NonDistinctMultitouchHelper.processMotionEvent(NonDistinctMultitouchHelper.java:56)
08-06 10:57:19.176  2428  2428 W System.err:    at com.android.inputmethod.keyboard.MainKeyboardView.onTouchEvent(MainKeyboardView.java:714)
08-06 10:57:19.176  2428  2428 W System.err:    at com.android.inputmethod.latin.LatinIME$2.run(LatinIME.java:2021)
08-06 10:57:19.176  2428  2428 W System.err:    at android.os.Handler.handleCallback(Handler.java:873)
08-06 10:57:19.176  2428  2428 W System.err:    at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 10:57:19.176  2428  2428 W System.err:    at android.os.Looper.loop(Looper.java:193)
08-06 10:57:19.176  2428  2428 W System.err:    at android.app.ActivityThread.main(ActivityThread.java:6680)
08-06 10:57:19.176  2428  2428 W System.err:    at java.lang.reflect.Method.invoke(Native Method)

? ? ? ? IME_ACTION_NEXT的值就是5,也就是说在LatinIME中对enter键的处理就是使用IME_ACTION_NEXT。

解决方案

? ? ? ? 再次回归到项目中,项目所使用的输入法,显然与LatinIME不一样,虽然其也有ENTER键,但其他只是简单地当做一个普通的ENTER键进行处理。

? ? ? ? 此问题我思考了一天,在寻找一个好的处理方式,因为这个问题所在的布局和接口实现不仅仅只有一个问题,而是有好多处,需要想一个简单且只需修改公共的地方。

? ? ? ? 因此想了一下的几个方式:

? ? ? ? 1.修改framework中TextView方法中的onKeyDown方法,在这里将收到Enter键时触发IME_ACTION_NEXT。

? ? ? ? 2.修改TVSetting的布局,参考其他厂商的布局,在右侧增加确定和取消的button。

? ? ? ? 3.修改TvSetting中布局使用的EditText,自定义一个InfoEditText继承EditText,并override它的onKeyDown方法。

? ? ? ? 在前两个想法中折腾了挺久,后想到了第三个方法,更方便地解决了问题,修改如下:

? ? ? ? 自定义InfoEditText:

package com.android.tv.settings.connectivity.util;

import android.content.Context;
import android.widget.EditText;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.KeyEvent;
import android.util.Log;


public class InfoEditText extends EditText {
    public InfoEditText(Context context) {
        this(context, null);
    }

    public InfoEditText(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.editTextStyle);
    }

    public InfoEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public InfoEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (!isEnabled()) {
            return super.onKeyDown(keyCode, event);
        }
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            onEditorAction(EditorInfo.IME_ACTION_NEXT);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

? ? ? ? 在onKeyDown中判断,如果接收到Enter事件,则调用onEditorAction触发IME_ACTION_NEXT。

? ? ? ? 修改布局:

diff --git a/Settings/res/layout/setup_password_item.xml b/Settings/res/layout/setup_password_item.xml
index 64951483..a752dce4 100644
--- a/Settings/res/layout/setup_password_item.xml
+++ b/Settings/res/layout/setup_password_item.xml
@@ -32,7 +32,7 @@
         android:paddingEnd="@dimen/setup_list_item_padding"
         android:paddingStart="@dimen/setup_list_item_padding">

-        <EditText
+        <com.android.tv.settings.connectivity.util.InfoEditText
             android:id="@+id/guidedactions_item_title"
             style="@style/Setup.Action.TextInput"
             android:layout_width="match_parent"
@@ -41,7 +41,7 @@
             android:gravity="center_vertical"
             android:imeOptions="actionNext|flagNoExtractUi"
             android:inputType="text">
-        </EditText>
+        </com.android.tv.settings.connectivity.util.InfoEditText>
     </FrameLayout>

     <CheckBox
@@ -53,4 +53,4 @@
         android:buttonTint="@color/setup_list_item_background_focused"
         android:buttonTintMode="src_in"
         android:text="@string/text_obfuscation_toggle_caption" />
-</LinearLayout>
\ No newline at end of file
+</LinearLayout>
diff --git a/Settings/res/layout/setup_text_input_item.xml b/Settings/res/layout/setup_text_input_item.xml
index a178b17d..3ef4d9ae 100644
--- a/Settings/res/layout/setup_text_input_item.xml
+++ b/Settings/res/layout/setup_text_input_item.xml
@@ -31,7 +31,7 @@
         android:paddingEnd="@dimen/setup_list_item_padding"
         android:paddingStart="@dimen/setup_list_item_padding">

-        <EditText
+        <com.android.tv.settings.connectivity.util.InfoEditText
             android:id="@+id/guidedactions_item_title"
             style="@style/Setup.Action.TextInput"
             android:layout_width="match_parent"
@@ -40,7 +40,7 @@
             android:gravity="center_vertical"
             android:imeOptions="actionNext|flagNoExtractUi"
             android:inputType="text">
-        </EditText>
+        </com.android.tv.settings.connectivity.util.InfoEditText>
     </LinearLayout>

     <TextView
@@ -51,4 +51,4 @@
         android:layout_toEndOf="@id/text_input_wrapper"
         android:textAppearance="?android:attr/textAppearanceListItem"
         android:visibility="gone" />
-</RelativeLayout>
\ No newline at end of file
+</RelativeLayout>

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-07 21:47:27  更:2021-08-07 21:47:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 13:20:36-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码