【问题标题】:OnEditorActionListener with imeOptions actionNext not working带有 imeOptions actionNext 的 OnEditorActionListener 不起作用
【发布时间】:2017-07-30 19:34:57
【问题描述】:

这是我的代码:

<android.support.design.widget.TextInputLayout
        android:id="@+id/mylayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/some_layout">
        <android.support.design.widget.TextInputEditText
            android:id="@+id/myid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="@string/some_hint"
            android:imeOptions="actionNext"
            android:inputType="time"
            android:maxLength="@integer/max_input_length"
            android:maxLines="1"
            android:singleLine="true"
            android:textSize="15sp"/>
    </android.support.design.widget.TextInputLayout>

和 Java 代码:

myField = (TextInputEditText) findViewById(R.id.myid);
    myField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                Log.d(TAG,"next");
                //Do something
                handled = true;
            }
            Log.d(TAG,"handled: "+handled);
            return handled;
        }
    });`

不幸的是,当我按下键盘上的下一个按钮时,没有任何反应。光标不会跳转到下一个字段。 我看不到我错过了什么

【问题讨论】:

    标签: android imeoptions


    【解决方案1】:

    android:inputType="text" 用作您的TextInputEditText

    尝试在您的操作中调用view.requestFocus();

    myField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                Log.d(TAG,"next");
                //Do something
                Log.d(TAG,"handled: "+handled);
                view.requestFocus() ;  //add focus to next view object
                return true;   //return true
            }
            Log.d(TAG,"handled: "+handled);
            return false;   //add return
        }
    });
    

    【讨论】:

    • 嗨@rafsanahmad007。我确实为我的特定操作设置了handled=true,然后由方法返回。
    【解决方案2】:

    根据文档

    IME_ACTION_NEXT

    IME_MASK_ACTION 位:操作键执行“下一个”操作, 将用户带到下一个接受文本的字段。

    这意味着它将专注于下一个可聚焦的对象,例如edittext或自动完成文本视图。所以如果没有其他对象可以获得焦点,它就不会移动焦点。

    【讨论】:

    • 嗨@androidnoobdev,对象是TextInputLayout ,就像上面的一样。所以我认为它应该能够获得焦点。
    • @DeKekem 然后添加此属性 - android:focusable="true" android:focusableInTouchMode="true" 并立即检查。
    • 也;这是一个巨大的错误:如果该字段隐藏在软键盘后面,IME_ACTION_NEXT 不会导致跳转到下一个字段。我今天向 ANDROID R/D 发送了错误报告。
    猜你喜欢
    • 2014-05-27
    • 2013-02-18
    • 2015-01-16
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多