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

EditText OnKeyListener 不起作用

EditText OnKeyListener not working(EditText OnKeyListener 不起作用)
本文介紹了EditText OnKeyListener 不起作用的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我知道 this/similar question 之前已被問(wèn)過(guò),但解決方案given 對(duì)我不起作用,所以我再次詢(xún)問(wèn).我嘗試了該答案中給出的解決方案,但我的 OnKeyListener 仍然從未在某些設(shè)備上被調(diào)用,尤其是那些具有股票操作系統(tǒng)的設(shè)備.當(dāng)editText中沒(méi)有字符時(shí),我需要檢測(cè)是否按下軟鍵盤(pán)的del鍵.這是我的代碼;

I know this/similar question has been asked before but the solution given is not working for me so I'm asking again. I tried the solution given in that answer but still my OnKeyListener is never being invoked on some devices, especially the ones with stock OS. I need to detect pressing of del key of soft keyboard when when there is no character in editText. Here is my code;

EditText et = (EditText) findViewById(R.id.et);
    et.setOnKeyListener(new EditText.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Log.d("OnKeyListener", keyCode + " character(code) to send");
            return false;
        }
    });

推薦答案

終于自己解決了,通過(guò)TextWatcher實(shí)現(xiàn)這個(gè)功能.主要障礙是,即使 EditText 中沒(méi)有字符,或者至少最終用戶(hù)認(rèn)為那里沒(méi)有字符,我也需要檢測(cè)退格鍵.第一件事無(wú)法實(shí)現(xiàn),但我做了后一個(gè).以下是詳細(xì)的解決方案.

Finally solved myself by implementing this feature through TextWatcher. The major hurdle was that, I needed to detect backspace press even when there is no character in EditText or at least the end user perceives that there is no character there. The fist thing couldn't be achieved however I did the later one. Following is the detailed solution.

首先,我的 editText 中總是保留一個(gè)空格 ' ' 字符.

First of all, I always retained a space ' ' character in my editText.

editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        if(cs.toString().length() == 0)
            editText.setText(" ");
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }

    @Override
    public void afterTextChanged(Editable arg0) { }

});

然后我自定義了 EditText 來(lái)通知我每次光標(biāo)位置的變化.這個(gè)目的是通過(guò)重寫(xiě) EditTextonSelectionChanged 方法來(lái)實(shí)現(xiàn)的.我自定義的 EditText 是這樣的.

Then I customized EditText to notify me for every cursor position change. This purpose is achieved by overriding onSelectionChanged method of EditText. My customized EditText looks like this.

public class SelectionEnabledEditText extends EditText {
    public SelectionEnabledEditText(Context context) {
        super(context);
    }

    public SelectionEnabledEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SelectionEnabledEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);

        if(onSelectionChangeListener != null)
            onSelectionChangeListener.onSelectionChanged(selStart, selEnd);
    }

    public static interface OnSelectionChangeListener{
        public void onSelectionChanged(int selStart, int selEnd);
    }

    private  OnSelectionChangeListener onSelectionChangeListener;

    public void setOnSelectionChangeListener(OnSelectionChangeListener onSelectionChangeListener) {
        this.onSelectionChangeListener = onSelectionChangeListener;
    }
}

最后,在我的活動(dòng)中,我正在監(jiān)聽(tīng)光標(biāo)位置更改事件并在 editText 中重置我的光標(biāo)位置,如果它在必要的空格字符開(kāi)始處,即在第 0 個(gè)索引處,像這樣;

Finally, in my activity, I'm listening for cursor position changed event and resetting my cursor position in editText if it's there at the necessary space charatcer start i.e. at 0th index, like this;

editText.setOnSelectionChangeListener(new SelectionEnabledEditText.OnSelectionChangeListener() {
    @Override
    public void onSelectionChanged(int selStart, int selEnd) {
        if (selEnd == 0) {
            if (editText.getText().toString().length() == 0)
                editText.setText(" ");

            editText.setSelection(1);
        }
    }
});

希望這對(duì)類(lèi)似情況有所幫助.歡迎提出改進(jìn)/優(yōu)化建議.

Hope this would be helpful in similar situations. Suggestions are welcomed for improvements/optimizations.

這篇關(guān)于EditText OnKeyListener 不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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 操作標(biāo)簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測(cè)向左或向右滑動(dòng)?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉(zhuǎn)對(duì)話(huà)框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點(diǎn)擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設(shè)置為僅接受 Android 中的數(shù)值?)
主站蜘蛛池模板: 国产精品久久久久久久午夜片 | 国产精品成人69xxx免费视频 | 日韩www| 日本精品裸体写真集在线观看 | 亚洲狠狠爱 | 97精品国产97久久久久久免费 | 精品国产一区二区三区性色av | 中文字幕av亚洲精品一部二部 | 福利视频网 | 国产色网 | 国产成人在线一区 | 色片在线观看 | 久久最新 | 九九热精品在线 | 国产成人免费视频网站高清观看视频 | 成人免费一区二区三区视频网站 | 中文字幕第90页 | 久久51 | 国产999精品久久久久久 | 亚洲a在线观看 | 一级在线 | 久久精品小视频 | 国产精品久久网 | 一区二区三区视频在线 | 天堂网avav| 日韩成人免费av | 亚洲国产欧美一区二区三区久久 | 亚洲国产视频一区二区 | 亚洲视频在线免费观看 | 久久亚洲国产精品日日av夜夜 | 久久久久久久国产精品视频 | 欧美嘿咻 | 久久精品 | a在线观看 | 国产乱精品一区二区三区 | 国产成人一区二区三区精 | 91极品欧美视频 | 久久最新精品 | 欧美三级成人理伦 | 日本网站免费在线观看 | 久草.com |