这里是我的编辑文本:-
<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" />
当我在 edittext 中删除 digit
时,它工作正常但 digit
imeOptions
不起作用。但令人惊讶的是,如果我使用 singleLine
而不是 maxLines,它工作正常。但是 singleLine
现在已被弃用。 我无法删除编辑文本中的数字,我不想使用已弃用的方法。任何人都可以解决这个问题。提前致谢
最佳答案
这是一个使用软件键盘按钮“下一步”的简化解决方案:
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]+
对应于EditText
的android:digits
的允许值> 有问题。
关于android - 使用数字时,将 EditText imeOptions 设置为 actionNext 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45351481/