久久久久久久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的网址 | 99精品一区二区三区 | 国产在线高清 | 国产欧美日韩在线 | 国产xxxx岁13xxxxhd | 中文字幕在线观看一区 | 91婷婷韩国欧美一区二区 | 91精品久久久久久久久久小网站 | 91 在线| 最新免费视频 | 国产欧美一区二区精品忘忧草 | 欧美久久久久久久 | 97国产在线观看 | 91高清在线视频 | 国产精品久久久久久久久久久免费看 | 一区二区三区在线 | 欧美男人天堂 | a在线观看 | 粉嫩一区二区三区国产精品 | 紧缚调教一区二区三区视频 | 91久久久久 | 二区av| 日日干夜夜操 | 日日操日日干 | 综合色在线 | 欧美日韩一区二区三区四区 | 国产成人叼嘿视频在线观看 | 91在线资源 | 精品国产精品一区二区夜夜嗨 | 一区二区成人 | 日韩精品区 | 精品久久久久久久久久久久久 | 日本在线视频一区二区 | 欧美中文字幕在线观看 | 久久亚洲综合 | 久久综合成人精品亚洲另类欧美 | 国产精品黄色 | 日韩精品免费播放 | 在线黄色影院 | 最新91在线 | 欧美日韩国产一区二区三区 |