本文介绍了Android的发展:如何使用的onkeyup?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid开发,我似乎无法找到如何使用的onkeyup 听者很好的指导作用。

I'm new to Android development and I can't seem to find a good guide on how to use an onKeyUp listener.

在我的应用程序,我有一个大的的EditText ,当有人presses并释放在一个关键的EditText 我要调用将在执行定期EX pressions功能的EditText

In my app, I have a big EditText, when someone presses and releases a key in that EditText I want to call a function that will perform regular expressions in that EditText.

我不知道我怎么会用的onkeyup。可能有人请告诉我如何?

I don't know how I'd use the onKeyUp. Could someone please show me how?

推荐答案

非常正确的方法是使用TextWatcher类。

The very right way is to use TextWatcher class.

EditText tv_filter = (EditText) findViewById(R.id.filter);

TextWatcher fieldValidatorTextWatcher = new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (filterLongEnough()) {
                populateList();
            }
        }

        private boolean filterLongEnough() {
            return tv_filter.getText().toString().trim().length() > 2;
        }
    };
    tv_filter.addTextChangedListener(fieldValidatorTextWatcher);

这篇关于Android的发展:如何使用的onkeyup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 22:19