Android的发展:如何使用的onkeyup?如何使用、Android、onkeyup

2023-09-13 01:55:51 作者:小兔牙^ˇ^

我是新的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);