EditText & TextChangeListener

2019-04-15 20:57发布

问题:

I need yours help. I have EditText field, which acts as search field for searching thought many items in the list. Now I'm use afterTextChanged(Editable s) method of TextWatcher, but it's not perfect for me. Some times after fast input and erase next search process involve not all text inputed by the user. The reason is in long search process and I can't make it shorter. In my case I need to know, wnen user ends his input at all, but afterTextChanged() handle every sign change. I will appreciate any ideas. Thanks!

回答1:

I'm guessing you're using a TextWatcher because you want to make live searches. In that case you can't know when the user has finished input BUT you CAN limit the frequency of your searches.

Here's some sample code:

searchInput.addTextChangedListener(new TextWatcher()
{
    Handler handler = new Handler();
    Runnable delayedAction = null;

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

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

    @Override
    public void afterTextChanged( final Editable s)
    {
        //cancel the previous search if any
        if (delayedAction != null)
        {
            handler.removeCallbacks(delayedAction);
        }

        //define a new search
        delayedAction = new Runnable()
        {
            @Override
            public void run()
            {
                //start your search
                startSearch(s.toString());
            }
        };

        //delay this new search by one second
        handler.postDelayed(delayedAction, 1000);
    }
});

The only way to know if the input has ended is for the user to press enter or the search button or something. You can listen for that event with the following code:

searchInput.setOnEditorActionListener(new OnEditorActionListener()
{

    @Override
    public boolean onEditorAction( TextView v, int actionId, KeyEvent event)
    {
        switch (actionId)
        {
        case EditorInfo.IME_ACTION_SEARCH:
            //get the input string and start the search
            String searchString = v.getText().toString();
            startSearch(searchString);
            break;
        default:
            break;
        }
        return false;
    }
});

Just make sure to add android:imeOptions="actionSearch" to the EditText in the layout file.



回答2:

What you need is TextWatcher

http://developer.android.com/reference/android/text/TextWatcher.html



回答3:

How I usually do that is to use onFocusChange

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            // Do your thing here
        }
    }
});

This has the one drawback of the user having to move away from the edittext field though, so I'm not sure if it will fit in with what you are trying to do...