TextWatcher的AfterTextChanged对按下的每个键做出反应?

时间:2013-09-26 12:40:30

标签: android

我有一个编辑文本视图,我想在用户停止在字段中写入后立即进行下一次验证,所以我决定使用AfterTextChanged是合乎逻辑的事情。

问题是,AfterTextChanged在每次击键后运行我在paretheses中写的代码。

etUserPasswordSignupPage.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                //do some validation and then TOAST 
            }
        });

Toast显示多次,而我打字:( 如何在用户停止输入并转到另一个字段时等待,或者完成然后运行“afterTextChanged”的代码?

编辑:

这是在实现setOnEditorActionListener

之后发生的事情
09-26 17:15:36.275: E/AndroidRuntime(2102): FATAL EXCEPTION: main
09-26 17:15:36.275: E/AndroidRuntime(2102): java.lang.NullPointerException
09-26 17:15:36.275: E/AndroidRuntime(2102):     at net.shiftinpower.activities.Signup$4.onEditorAction(Signup.java:154)
09-26 17:15:36.275: E/AndroidRuntime(2102):     at android.widget.TextView.onEditorAction(TextView.java:3377)
09-26 17:15:36.275: E/AndroidRuntime(2102):     at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:83)
09-26 17:15:36.275: E/AndroidRuntime(2102):     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:301)
09-26 17:15:36.275: E/AndroidRuntime(2102):     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:79)

这是我的真实代码:

etUserPasswordAgainSignupPage.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
         if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            userPasswordAgain = etUserPasswordAgainSignupPage.getText().toString();

            if (userPasswordAgain.equals("")) {
                Toast.makeText(net.asdqwe.activities.Signup.this, configurationz.ERROR_MESSAGES_SIGNUP_FIELD_NOT_FILLED, Toast.LENGTH_SHORT).show();
            } else {
                passwordAgainIsOk = true;
                Log.d("kylie", "pass again is ok");
            }

            if (!(userPassword.equals(userPasswordAgain))) {
                Toast.makeText(net.asdqwe.activities.Signup.this, configurationz.ERROR_MESSAGES_SIGNUP_PASSWORDS_DO_NOT_MATCH, Toast.LENGTH_SHORT).show();
                passwordsMatch = false;
            } else {
                passwordsMatch = true;
                Log.d("kylie", "passwords match");
            }
        }
        return false;
    }
});

1 个答案:

答案 0 :(得分:2)

尝试以下代码....当EditText失去焦点时,此代码将立即触发onFocusChangeListener()

   EditText et = new EditText(this); 
   //or
   EditText et = (EditText)findViewById(R.id.editText);
   et.setOnFocusChangeListener(new OnFocusChangeListener() { 
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if(!hasFocus){
                //Validate the string entered in EditText
            }
        }
    });

     OR 

     et.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            // TODO Auto-generated method stub

            if (actionId == EditorInfo.IME_ACTION_DONE) {
                 //Do your validation thing here
            }
            return false; 
        }
    });