久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 TextWatcher 中更改文本后 EditText 未更新

EditText not updated after text changed in the TextWatcher(在 TextWatcher 中更改文本后 EditText 未更新)
本文介紹了在 TextWatcher 中更改文本后 EditText 未更新的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個 EditText 和一個 TextWatcher.

I have an EditText and a TextWatcher.

我的代碼骨架:

EditText x;
x.addTextChangedListener(new XyzTextWatcher());

XyzTextWatcher implements TextWatcher() {
    public synchronized void afterTextChanged(Editable text) {
        formatText(text);
    }
}

我的 formatText() 方法在文本的某些位置插入一些連字符.

My formatText() method inserts some hyphens at some positions of the text.

private void formatText(Editable text) {
    removeSeparators(text);

    if (text.length() >= 3) {
        text.insert(3, "-");
    }
    if (text.length() >= 7) {
        text.insert(7, "-");
    }
}

private void removeSeparators(Editable text) {
    int p = 0;
    while (p < text.length()) {
        if (text.charAt(p) == '-') {
            text.delete(p, p + 1);
        } else {
            p++;
        }
    }
}

我遇到的問題是 - 我的 EditText 上顯示的內容與 Editable 不同步.當我調試代碼時,我看到變量 text (Editable) 具有預期值,但 EditText 上顯示的內容并不總是與 Editable 匹配.

The problem I have is - what is displayed on my EditText isn't in sync with the Editable. When I debugged the code, I saw that the variable text (Editable) has the expected value, but what's shown on the EditText doesn't always match the Editable.

例如,當我有一個文本x = "123-456-789"我手動從 x 中剪切了文本456".格式化后,我的 Editable 的值為123-789-"但是,我的 EditText 上顯示的值是123--789"

For example, when I have a text x = "123-456-789" I cut the text "456" from x manually. After formatting, my Editable has the value "123-789-" However, the value shown on my EditText is "123--789"

但在大多數情況下,它們具有相同的值.

They have the same value in most cases though.

我假設 EditText 是 Editable 并且它們應該始終匹配.我錯過了什么嗎?

I assumed that the EditText IS the Editable and they should always match. Am I missing something?

推薦答案

好吧,你從來沒有真正改變 EditText 只是 Editable.Android EditTexts 不是 Editable 類的子級.字符串是 Editable 類的子類.onTextChangedListener 不接收 EditText 作為參數,而是接收 EditText 中顯示的 Editable/String.使用連字符格式化 Editable 后,您需要更新 EditText.這樣的事情應該可以正常工作:

Ok, you never actually change the EditText just the Editable. Android EditTexts are not children of the Editable class. Strings are subclasses of the Editable class. The onTextChangedListener doesn't receive the EditText as an argument but the Editable/String displayed in the EditText. After you format the Editable with the hyphens you then need to update the EditText. Something like this should work fine:

class MyClass extends Activity{

    //I've ommited the onStart(), onPause(), onStop() etc.. methods

    EditText x;
    x.addTextChangedListener(new XyzTextWatcher());

    XyzTextWatcher implements TextWatcher() {
        public synchronized void afterTextChanged(Editable text) {
            String s = formatText(text);
            MyClass.this.x.setText(s);
        }
    }

}

為了防止速度變慢,為什么不改變 formatText 方法呢?

To prevent the slowdown why not change the formatText method something like this?

private Editable formatText(Editable text) {
    int sep1Loc = 3;
    int sep2Loc = 7;

    if(text.length==sep1Loc)
    text.append('-');

    if(text.length==sep2Loc)
    text.append('-');

    return text;
}

注意:我沒有測試過這個

這篇關于在 TextWatcher 中更改文本后 EditText 未更新的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 久久在线| 久久精品二区 | 懂色av色香蕉一区二区蜜桃 | 涩涩片影院 | 黄色亚洲网站 | 国产欧美一级二级三级在线视频 | 国产日韩av一区二区 | 欧美日韩精品在线一区 | 999久久精品 | 日韩视频在线观看 | 一级黄色片美国 | 日本成人中文字幕 | 久久99久久| 国产精品亚洲欧美日韩一区在线 | 免费a级毛片在线播放 | 国产精品成人在线播放 | 97色伦网 | 国内精品视频一区二区三区 | 98成人网 | 成人av鲁丝片一区二区小说 | 久久99精品久久久 | 一区二区三区视频在线 | 国产精品美女久久久久久久网站 | av在线一区二区 | 欧美视频免费在线 | 欧美a在线看 | 欧美一级在线视频 | 日韩视频在线一区 | 欧美无乱码久久久免费午夜一区 | 日本成人午夜影院 | 玖玖在线精品 | 国产一区二区在线播放 | 色综合久久久久 | 日韩一区二区三区在线视频 | 亚洲精品字幕 | av在线成人| 91精品国产欧美一区二区成人 | 国产日韩精品视频 | 欧美激情一区二区三区 | 欧美日韩亚洲国产 | 超碰在线免费av |