Knowing when Edit text is done being edited

2019-01-10 04:55发布

How do I know when my edit text is done being edited? Like when the user selects the next box, or presses the done button on the soft keyboard.

I want to know this so I can clamp the input. It looks like text watcher's afterTextChanged happens after each character is entered. I need to do some calculations with the input, so I would like to avoid doing the calculation after each character is entered.

thanks

5条回答
smile是对你的礼貌
2楼-- · 2019-01-10 04:58

Using an EditText object xml defined like this:

    <EditText
        android:id="@+id/create_survey_newquestion_editText_minvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:ems="4"
        android:imeOptions="actionDone"
        android:inputType="number" />

We can capture its text i) when the user clicks the Done button on the soft keyboard (OnEditorActionListener) or ii) when the EditText has lost the user focus (OnFocusChangeListener) which now is on another EditText:

    /**
     * 3. Set the min value EditText listener
     */
    editText= (EditText) this.viewGroup.findViewById(R.id.create_survey_newquestion_editText_minvalue);
    editText.setOnEditorActionListener(new OnEditorActionListener()
    {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
        {
            String input;
            if(actionId == EditorInfo.IME_ACTION_DONE)
            {
                input= v.getText().toString();
                MyActivity.calculate(input);
                return true; // consume.
            }
            return false; // pass on to other listeners.
        }
    });
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View v, boolean hasFocus)
        {
            String input;
            EditText editText;

            if(!hasFocus)
            {
                editText= (EditText) v;
                input= editText.getText().toString();
                MyActivity.calculate(input);
            }
        }
    });

This works for me. You can hide the soft keyboard after made the calculations using a code like this:

private void hideKeyboard(EditText editText)
{
    InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

Edit: added return values to onEditorAction

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-10 05:02

EditText inherits setOnFocusChangeListener which takes an implementation of OnFocusChangeListener.

Implement onFocusChange and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.

EDIT

To handle both cases - edit text losing focus OR user clicks "done" button - create a single method that gets called from both listeners.

    private void calculate() { ... }

    btnDone.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            calculate();
        }
    });

    txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {          

        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus)
                calculate();
        }
    });
查看更多
Luminary・发光体
4楼-- · 2019-01-10 05:07

By using something like this

 meditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                yourcalc();

                return true;
            }
            return false;
        }
    });
查看更多
Bombasti
5楼-- · 2019-01-10 05:07

To be more highlevel you may use TextWatcher's afterTextChanged() method.

查看更多
Animai°情兽
6楼-- · 2019-01-10 05:08

I have done this simple thing, when focus is shifted from edit text get the text. This will work if user shifts the focus to select other views like a button or other EditText or any view.

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                EditText editText = (EditText) v;
                String text = editText.getText().toString();
             }
        }
    });
查看更多
登录 后发表回答