Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

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

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 ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...