将 EditText imeOptions 设置为 actionNext 在使用数字时没有效果

问题描述 投票:0回答:6

这是我的编辑文本:-

    <com.os.fastlap.util.customclass.EditTextPlayRegular
                    android:id="@+id/full_name_et"
                    style="@style/Edittext_white_13sp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_5sdp"
                    android:background="#00ffffff"
                    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                    android:imeOptions="actionNext"
                    android:inputType="text"
                    android:maxLength="20"
                    android:maxLines="1"
                    android:nextFocusDown="@+id/last_name_et"
                    android:textCursorDrawable="@null" /> 

当我在编辑文本中删除

digit
时,它工作正常,但使用
digit
imeOptions
则不起作用。但令人惊讶的是,如果我使用
singleLine
而不是 maxLines ,它就可以正常工作。但
singleLine
现在已被弃用。 我无法删除编辑文本中的数字,并且我不想使用已弃用的方法。任何人都可以解决这个问题。提前致谢

java android regex android-edittext
6个回答
3
投票

这是使用软件键盘按钮“下一步”的简化解决方案:

final String NOT_ALLOWED_CHARS = "[^a-zA-Z0-9]+";

final EditText editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s)) {
                // remove the listener to avoid StackoverflowException
                editText.removeTextChangedListener(this);
                // replace not allowed characters with empty strings
                editText.setText(s.toString().replaceAll(NOT_ALLOWED_CHARS, ""));
                // setting selection moves the cursor at the end of string
                editText.setSelection(editText.getText().length());
                // add the listener to keep watching
                editText.addTextChangedListener(this);
            }
        }
    });

这里正则表达式

[^a-zA-Z0-9]+
对应于相关
android:digits
EditText
的允许值。


2
投票

你可以使用

 android:lines="1"

代替

android:maxLines 

android:singleLine="true".

我知道你已经尝试了很多解决方案,请尝试使用

android:lines="1"

如果您之前没有尝试过,请尝试这些。


1
投票

问题是您单击 Enter 键而不是所需的 actionNext 以便将光标移动到下一个 EditText

指定输入法操作

大多数软输入法在底部提供用户操作按钮 适合当前文本字段的角。默认情况下, 系统使用此按钮执行“下一步”或“完成”操作,除非您 文本字段允许多行文本(例如 android:inputType="textMultiLine"),在这种情况下操作按钮是 回车。但是,您可以指定其他操作 可能更适合您的文本字段,例如“发送”或“前往”。

它会导致您的操作按钮回车。所以这意味着不会触发

android:nextFocusDown

首先,让我们看看已弃用的 singleLine 和 maxLines 之间有什么区别

单线

当您设置

android:singleLine="true"
时,一行文本在EditText中可见,但Enter键在键盘中不可见

最大行数

当您使用特定值设置

android:maxLines
属性时,只有相同数量的行文本在 EditText 中可见,并且键盘中的输入键对于 Entering 也可见。

因此,当您单击操作按钮时,它会根据您的代码触发 Enter Action。另外,如果您使用

android:inputType="textMultiLine"
属性
,则必须使用 
android:maxLines

更改 inputType 属性

最大行数

在 API 级别 1 中添加 int maxLines 使 TextView 最多为这个 许多行高。当用于可编辑文本时, inputType 属性的值必须与 textMultiLine 标志结合使用 要应用的 maxLines 属性。

可以是整数值,例如“100”。


当我使用正确的属性自定义您的代码时,它仍然触发 Enter 键而不是您想要的 IME_ACTION_NEXT 。我认为这没有解决问题,因为

textMultiLine 可以与文本及其变体组合以允许 字段中的多行文本。如果未设置此标志,则文本 字段将被限制为一行。对应于 TYPE_TEXT_FLAG_MULTI_LINE。


TYPE_TEXT_FLAG_MULTI_LINE

在 API 级别 3 中添加了 int TYPE_TEXT_FLAG_MULTI_LINE 标志 TYPE_CLASS_TEXT:可以在字段中输入多行文本。 如果未设置此标志,则文本字段将被限制为 单线。 IME 也可能选择在以下情况下不显示回车键: 未设置此标志,因为不需要创建新行。

常量值:131072 (0x00020000)


解决方案:

子类化 EditText 并调整 IME 选项。之后,您不需要

android:maxLines
android:singleLine
属性。

@Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        InputConnection connection = super.onCreateInputConnection(outAttrs);
        int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
        if ((imeActions&EditorInfo.IME_ACTION_NEXT) != 0) {
            // clear the existing action
            outAttrs.imeOptions ^= imeActions;
            // set the DONE action
            outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT;
        }
        if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
            outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
        }
        return connection;
    }

您还可以查看另一篇文章这里。我根据您的目的重新配置了这篇文章接受的答案


0
投票

android:digits
指定 EditText 有数字输入方法,根据 docs

您可以使用

android:inputType="personName"
来实现与当前
digits
attr

相同的效果

0
投票

android:maxLines
只是让 TextView 最多这么多行,不阻止用户输入更多字符。

android:lines
相同,只会使 TextView 恰好这么多行高。


0
投票

您的解决方案的解决方法代码如下,

XML 文件

    <EditText
            android:id="@+id/full_name_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5sp"
            android:background="#00ffffff"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:maxLines="1"
            android:maxLength="20"
            android:textCursorDrawable="@null" />

Java:

final EditText edtfirstName = (EditText) findViewById(R.id.full_name_et);


    edtfirstName.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
            if (charSequence.toString().startsWith(" ")) {
                String result = charSequence.toString().replace(" ", "").replaceAll("[^a-zA-Z]+", "");
                if (!charSequence.toString().equals(result)) {
                    edtfirstName.setText(result);
                    edtfirstName.setSelection(result.length());
                }
            } else {
                String result = charSequence.toString().replaceAll("[^a-zA-Z ]+", "");
                if (!charSequence.toString().equals(result)) {
                    edtfirstName.setText(result);
                    edtfirstName.setSelection(result.length());
                }
            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.