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

EditText OnKeyListener 不起作用

EditText OnKeyListener not working(EditText OnKeyListener 不起作用)
本文介紹了EditText OnKeyListener 不起作用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我知道 this/similar question 之前已被問過,但解決方案given 對我不起作用,所以我再次詢問.我嘗試了該答案中給出的解決方案,但我的 OnKeyListener 仍然從未在某些設備上被調用,尤其是那些具有股票操作系統的設備.當editText中沒有字符時,我需要檢測是否按下軟鍵盤的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;
        }
    });

推薦答案

終于自己解決了,通過TextWatcher實現這個功能.主要障礙是,即使 EditText 中沒有字符,或者至少最終用戶認為那里沒有字符,我也需要檢測退格鍵.第一件事無法實現,但我做了后一個.以下是詳細的解決方案.

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 中總是保留一個空格 ' ' 字符.

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 來通知我每次光標位置的變化.這個目的是通過重寫 EditTextonSelectionChanged 方法來實現的.我自定義的 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;
    }
}

最后,在我的活動中,我正在監聽光標位置更改事件并在 editText 中重置我的光標位置,如果它在必要的空格字符開始處,即在第 0 個索引處,像這樣;

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);
        }
    }
});

希望這對類似情況有所幫助.歡迎提出改進/優化建議.

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

這篇關于EditText OnKeyListener 不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 中的數值?)
主站蜘蛛池模板: 国产成人精品一区二区在线 | 国产中文字幕亚洲 | 日韩精品一区二区三区免费视频 | 日韩一区二区三区av | 精品自拍视频 | 欧美一区二区 | 国产欧美精品区一区二区三区 | 成人精品国产 | 久久精品视频亚洲 | 成年免费在线观看 | 久久久久久久久久一区二区 | 日韩一区二区在线视频 | 日韩精品免费视频 | 国产精品av久久久久久久久久 | 久久久久亚洲精品 | 一区二区视频在线观看 | 久久精品国产一区二区电影 | 一区二区三区亚洲精品国 | 国产在线视频一区 | www中文字幕 | 一级欧美视频 | 国产精品一区二区日韩 | 久婷婷 | 911影院 | 黄色片网站在线观看 | 久久丁香 | 免费a网站 | 亚洲精品一区二区三区丝袜 | 国产97视频在线观看 | 久久久久国产精品一区 | 韩日免费视频 | 亚洲欧美在线观看 | 婷婷久久精品一区二区 | 国产精品高潮呻吟久久av野狼 | 久久久久久久久久久久久91 | 成人在线观看免费视频 | 国产精品久久久久免费 | 九九在线精品视频 | 精品一区二区三区在线观看 | 黄色视频a级毛片 | 免费在线观看成年人视频 |