I have following code for my edittext formatting, since it can take any input I am not setting any input type:
我的编辑文本格式有以下代码,因为它可以接受我没有设置任何输入类型的任何输入:
if (cardNumberEditText != null) {
cardNumberEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int currSel = cardNumberEditText.getSelectionStart();
cardNumberEditText.removeTextChangedListener(textWatcher);
.
.
cardNumberEditText.setText(formattedNumber);
.
.
cardNumberEditText.setSelection(currSel);
cardNumberEditText.addTextChangedListener(textWatcher);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
So initially I get the default input type which is ABC, now when I change it to ?123 (using ABC/123? toggel button) and after entering some number the keyboard changes back to ABC. This code seams to work fine on samsung devices s3 and sywpe but not on nexus with L and HTC one
所以最初我得到的默认输入类型是ABC,现在当我将其更改为?123时(使用ABC/123?Togel按钮),并且在输入某个数字之后,键盘变回ABC。此代码似乎可以在三星设备S3和Sywpe上正常运行,但不能在与L和HTC One的联结上运行
When I comment all the code inside onTextChanged, it works fine. So when I investigated I found out that culprit is cardNumberEditText.setText(formattedNumber);
当我注释onTextChanged中的所有代码时,它工作得很好。所以当我调查时,我发现罪魁祸首是cardNumberEditText.setText(FormattedNumber);
I am not setting any input type, I am just using the ABC/?123 toggle key on keyboard for switching
我没有设置任何输入类型,我只是使用键盘上的ABC/?123切换键进行切换
Any help/suggestion why this is happening (on few devices) and how can I correct it ??
有什么帮助/建议吗?为什么会发生这种情况(在少数设备上)?我如何纠正它?
更多回答
Aren't you telling it to bring up the numeric keyboard by inputting a number? Or am I not understanding?
你不是告诉它通过输入数字来调出数字键盘吗?还是我没听懂?
no i am not telling it by writing any code for input type, I am using the keyboad key ABC/123 to switch between them
不,我不是通过为输入类型编写任何代码来告诉它,我是使用键盘键ABC/123在它们之间切换
finally found the answer here, will try it out
终于在这里找到了答案,来试试吧
unfortunately the above one doesn't work well with me, any other better solution ???
不幸的是,上面的方法对我不起作用,还有其他更好的解决方案吗?
found one more solution, using append instead of settext, but still doesn't work with my case, as I am doing card formatting I need to settext at the end
我找到了另一个解决方案,使用append而不是settext,但仍然不适用于我的情况,因为我正在进行卡片格式化,我需要在最后设置文本
优秀答案推荐
finnaly got it working, had to combine multiple solutions mentioned in the comments above
最终让它工作,不得不结合以上评论中提到的多个解决方案
since the guilty was settext, I found a replacement for it - append
既然有罪是定论,我就找了个替代的--追加
but to use append I had to clear edittext without using settext, this link to the rescue
但要使用append,我必须清除编辑文本,而不使用settext,这是指向救援的链接
so replaced
所以被取代了
cardNumberEditText.setText(formattedNumber);
with
使用
cardNumberEditText.getText().clear();
cardNumberEditText.append(formattedNumber);
works like a charm now
现在工作起来像个护身符
I had a similar problem, but keyboard was changed to default after I call
scrollView.fullScroll(NestedScrollView.FOCUS_DOWN)
when keyboard is shown
I just replaced fullScroll
method to smoothScrollTo
and all work fine
我也有类似的问题,但在我调用scrollView.fullScroll(NestedScrollView.FOCUS_DOWN)之后,键盘被更改为默认的,当键盘显示时,我只是替换了FullScroll方法来平滑ScrollTo,所有的工作都很好
In addition to the accepted answer, if anyone else struggles with cursor moving to the end after .append()
, here is how you can retain cursor position:
除了公认的答案之外,如果其他任何人在将光标移动到.append()之后的末尾时遇到困难,下面是如何保持光标位置的方法:
val cursor = editText.selectionEnd
editText.text?.clear()
editText.append(input)
editText.setSelectionSafe(cursor)
where setSelectionSafe()
is an extension I use to prevent crushes
其中setSelectionSafe()是我用来防止崩溃的扩展
fun EditText?.setSelectionSafe(index: Int) {
if (this == null || index > this.length()) return
this.setSelection(index)
}
this works both for entering and deleting !
这对输入和删除都有效!
更多回答
append helped me in my scenerio.->edittext which has some a input enetered.settext will remove/clears the entire input.replacing with new settext value. use append if you dont want input to be removed.
Append在我的场景中帮助了我。->带有一些需要的输入的编辑文本。settext将删除/清除整个输入。替换为新的setext值。如果不希望删除输入,请使用Append。
Unfortunately this causes Galaxy S III 4.3 not to clear previous text properly and buggy device appends the text twice, but I get good results with s.replace(0, s.length(), formatted, 0, formatted.length());
in afterTextChanged(Editable s)
.
遗憾的是,这会导致Galaxy S III 4.3无法正确清除之前的文本,并且BUGGY设备会两次添加文本,但我在After TextChanged(可编辑S)中使用s.place(0,s.long(),Formatted,0,Formatted.Long());获得了良好的结果。
Append method is fixing the keyboard switching behaivor. But there is a minor problem. My edittext is numeric because I want to prevent user entering anything but numbers. When I used settext I could set a text that contains space character. When I use append it doesn't allow me to append anything but numbers 8(
附加方法是修复键盘切换行为。但有一个小问题。我的编辑文本是数字的,因为我想防止用户输入除数字以外的任何内容。当我使用setText时,我可以设置包含空格字符的文本。当我使用APPEND时,它不允许我附加数字8以外的任何东西(
@EvrenOzturk this will be fixed by adding android:digits="0123456789 "
to the xml, apparently whatever you append gets filtered with android:inputType
but the filter will be "OR"ed with android:digits
@EvrenOzturk可以通过将android:digits=“0123456789”添加到xml来修复这一问题,显然,您追加的任何内容都将使用android:inputType进行过滤,但过滤器将使用android:digits进行“OR”处理。
我是一名优秀的程序员,十分优秀!