Android中的keyReleased等价 [英] KeyReleased equivalence in Android

查看:164
本文介绍了Android中的keyReleased等价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PC,我可以添加一个onKeyListener为一个JTextField听的keyReleased事件。在Android上我用addTextChangedListener。

On PC I can add a onKeyListener for a JTextField to listen keyReleased event. On Android I've used addTextChangedListener.

我在我的Andr​​oid适用的两个领域的EditText。编辑一个会影响到其他。这将导致程序在堆栈溢出错误失败。

I have two EditText fields in my Android application. Editing one will affect the other. This will cause the program to fail in stack overflow error.

我如何监听手机的键盘,而不是在外地的EditText变化?我不希望程序来调用,因为引起听者无限循环的听众。

How can I listen for the phone's keyboard instead of changes in the EditText field? I don't want the program to invoke the listener because of the infinite loop caused by the listener.

推荐答案

附加onFocusChangedListener并添加TextChangedListener时的EditText具有焦点,当它失去焦点将其删除。
事情是这样的:

Attach a onFocusChangedListener and add the TextChangedListener when a EditText has focus and remove it when it loses focus. Something like this:

 EditText1.setOnFocusChangeListener(new OnFocusChangeListener() {

                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus){
                        ((EditText) v).addTextChangedListener(new TextWatcher() {

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

                        }

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

                        }

                        public void afterTextChanged(Editable s) {
                            // affect EditText2

                        }
                    });

                }
                if(!hasFocus){
                    ((EditText) v).removeTextChangedListener();
                }
            }
        });

        }
    });

同为EditText2

The same for EditText2

这篇关于Android中的keyReleased等价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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