0

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

问题是,AfterTextChanged 在每次击键后都会运行我在括号中编写的代码。

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;
    }
});
4

1 回答 1

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; 
        }
    });
于 2013-09-26T12:48:21.530 回答