edittext.settext() changes the keyboard type to default [ from ?123 to ABC]

妖精的绣舞 提交于 2019-11-28 03:08:48

问题


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

When I comment all the code inside onTextChanged, it works fine. So when I investigated I found out that culprit is cardNumberEditText.setText(formattedNumber);

I am not setting any input type, I am just using the ABC/?123 toggle key on keyboard for switching

Any help/suggestion why this is happening (on few devices) and how can I correct it ??


回答1:


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

so replaced

cardNumberEditText.setText(formattedNumber);

with

cardNumberEditText.getText().clear();
cardNumberEditText.append(formattedNumber);

works like a charm now



来源:https://stackoverflow.com/questions/26365808/edittext-settext-changes-the-keyboard-type-to-default-from-123-to-abc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!