EditText上OnKeyListener不工作[解决了解决方法] [英] EditText OnKeyListener not working [SOLVED with workaround]

查看:1835
本文介绍了EditText上OnKeyListener不工作[解决了解决方法]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道<一个href="http://stackoverflow.com/questions/4282214/onkeylistener-not-working-on-virtual-keyboard">this/similar问题已经被问过,但给出的解决方案是不是为我工作,所以我问了。 我想在这个问题的答案中给出的解决方案,但仍然是我OnKeyListener永远不会被某些设备上,特别是那些与股票的操作系统调用。我需要检测的软键盘del键的pressing时,当在EDITTEXT没有字符。这是我的code;

 的EditText等=(EditText上)findViewById(R.id.et);
    et.setOnKeyListener(新EditText.OnKeyListener(){

        @覆盖
        公共布尔onKey(视图V,INT关键code,KeyEvent的事件){
            Log.d(OnKeyListener,关键code +符号(code)派);
            返回false;
        }
    });
 

解决方案

最后通过实现此功能通过 TextWatcher 解决自己。主要的障碍是,我需要检测退格preSS即使没有字符的EditText 或至少最终用户感知到没有字符出现。拳头的事情无法实现,但是我做了后一个。以下是详细的解决方案。

所有的

首先,我始终保留一个空格''字在我的 EDITTEXT

  editText.addTextChangedListener(新TextWatcher(){

    @覆盖
    公共无效onTextChanged(CharSequence的CS,诠释ARG1,诠释ARG2,诠释ARG3){
        如果(cs.toString()长度()== 0)
            editText.setText();
    }

    @覆盖
    公共无效beforeTextChanged(CharSequence的arg0中,诠释ARG1,诠释ARG2,诠释ARG3){}

    @覆盖
    公共无效afterTextChanged(编辑为arg0){}

});
 

然后我定制的EditText 来通知我的每一个光标移动。这个目的是通过覆盖 onSelectionChanged 的EditText 的方法来实现。我的自定义的EditText 看起来是这样的。

 公共类SelectionEnabledEditText扩展的EditText {
    公共SelectionEnabledEditText(上下文的背景下){
        超(上下文);
    }

    公共SelectionEnabledEditText(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    公共SelectionEnabledEditText(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
    }

    @覆盖
    保护无效onSelectionChanged(INT SelStart的,INT selEnd){
        super.onSelectionChanged(SelStart的,selEnd);

        如果(onSelectionChangeListener!= NULL)
            onSelectionChangeListener.onSelectionChanged(SelStart的,selEnd);
    }

    公共静态接口OnSelectionChangeListener {
        公共无效onSelectionChanged(INT SelStart的,INT selEnd);
    }

    私人OnSelectionChangeListener onSelectionChangeListener;

    公共无效setOnSelectionChangeListener(OnSelectionChangeListener onSelectionChangeListener){
        this.onSelectionChangeListener = onSelectionChangeListener;
    }
}
 

最后,在我的活动,我在听的光标位置改变事件和重置我的光标位置 EDITTEXT 如果它存在的必要的空间charatcer开始即在第0指数,像这样的;

  editText.setOnSelectionChangeListener(新SelectionEnabledEditText.OnSelectionChangeListener(){
    @覆盖
    公共无效onSelectionChanged(INT SelStart的,INT selEnd){
        如果(selEnd == 0){
            如果(editText.getText()的toString()长度()== 0)
                editText.setText();

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

希望这将是在类似情况下有所帮助。建议是欢迎的改进/优化。

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

解决方案

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.

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

});

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

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不工作[解决了解决方法]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆