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

TextWatcher 用于多個 EditText

TextWatcher for more than one EditText(TextWatcher 用于多個 EditText)
本文介紹了TextWatcher 用于多個 EditText的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想為多個EditText 字段實現TextWatcher 接口.目前我正在使用:

I want to implement the TextWatcher interface for more than one EditText fields. Currently I am using :

text1.addTextChangedListener(this);
text2.addTextChangedListener(this);

然后覆蓋我的 Activity 中的方法:

then overriding the methods in my Activity:

public void afterTextChanged(Editable s) {}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) 
{
 // do some operation on text of text1 field
 // do some operation on text of text2 field 
}

但是這工作正常,但我正在尋找其他方法,以便我可以明確識別 SoftKeyboard 當前關注的 EditText 字段.

However this is working fine but I'm looking for other ways so that I can explicitly identify that in which EditText field the SoftKeyboard is currently focused.

推薦答案

@Sebastian Roth 的回答中建議的解決方案不是一些 EditTextsTextWatcher 實例.對于 n EditTexts,它是一個類和該類的 n 個實例.

Suggested solution in @Sebastian Roth's answer is not one instance of TextWatcher for some EditTexts. It is one class and n instances of that class for n EditTexts.

每個 EditText 都有自己的 Spannable.TextWatcher 的事件有這個 Spannable 作為 s 參數.我檢查他們的 hashCode (每個對象的唯一 ID).myEditText1.getText() 返回 Spannable.因此,如果 myEditText1.getText().hashCode() 等于 s.hashCode() 這意味著 s 屬于 myEditText1

Each EditText has its own Spannable. TextWatcher's events has this Spannable as s parameter. I check their hashCode (unique Id of each object). myEditText1.getText() returns that Spannable. So if the myEditText1.getText().hashCode() equals with s.hashCode() it means that s belongs to myEditText1

因此,如果您想為某些 EditTexts 提供一個 TextWatcher 實例,您應該使用這個:

So if you want to have one instance of TextWatcher for some EditTexts you should use this:

private TextWatcher generalTextWatcher = new TextWatcher() {    

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {

        if (myEditText1.getText().hashCode() == s.hashCode())
        {
            myEditText1_onTextChanged(s, start, before, count);
        }
        else if (myEditText2.getText().hashCode() == s.hashCode())
        {
            myEditText2_onTextChanged(s, start, before, count);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

        if (myEditText1.getText().hashCode() == s.hashCode())
        {
            myEditText1_beforeTextChanged(s, start, count, after);
        }
        else if (myEditText2.getText().hashCode() == s.hashCode())
        {
            myEditText2_beforeTextChanged(s, start, count, after);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (myEditText1.getText().hashCode() == s.hashCode())
        {
            myEditText1_afterTextChanged(s);
        }
        else if (myEditText2.getText().hashCode() == s.hashCode())
        {
            myEditText2_afterTextChanged(s);
        }
    }

};

myEditText1.addTextChangedListener(generalTextWatcher);
myEditText2.addTextChangedListener(generalTextWatcher);

這篇關于TextWatcher 用于多個 EditText的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出)
Multiline EditText with Done SoftInput Action Label on 2.3(2.3 上帶有完成 SoftInput 操作標簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測向左或向右滑動?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設置為僅接受 Android 中的數值?)
主站蜘蛛池模板: 日韩免费视频一区二区 | 天天看天天爽 | 久久久久久毛片免费观看 | 日韩高清中文字幕 | 成人国产精品久久 | 精品国产一区二区三区四区在线 | 国产a区 | 欧美精品一区二区在线观看 | 国产成人精品免高潮在线观看 | 国产综合久久 | 一级毛片免费 | 色欧美综合 | 欧美一级视频 | 国产精品成人一区二区三区夜夜夜 | 日韩有码一区 | 国产网站在线播放 | 久草热8精品视频在线观看 午夜伦4480yy私人影院 | 91久久精品一区二区二区 | 久久高清精品 | 国产精品久久久久久久久久三级 | 91精品国产91久久久久久密臀 | 国产精品成人一区二区三区夜夜夜 | 日本在线视 | 精品欧美一区二区中文字幕视频 | 国产免费一区 | 亚洲精品av在线 | 欧美日韩国产不卡 | 精品国产一区二区在线 | 中文字幕免费视频 | 日本高清aⅴ毛片免费 | 亚洲精品视频免费观看 | 免费久久久 | 福利片在线观看 | 欧美日韩不卡合集视频 | 精品欧美二区 | 精品在线观看一区二区 | 久久婷婷香蕉热狠狠综合 | 中文天堂网 | 免费久久久| 在线播放91 | 久久国产精品久久久久久久久久 |