如何在android中的Edit文本中创建Textchanged Listener?

时间:2016-06-07 05:58:43

标签: android

我正在开发一个应用程序,其中我有两个字段:mobile和pass。当用户输入手机号码时,它会检查手机号码是否为十位数。如果没有,那么它应该在相应的字段中显示错误消息。但是,当应用程序打开时,它会在相应的字段中显示错误。我该如何解决这个问题?

这里是代码: -

private TextWatcher m_oTextWatcher = new TextWatcher() {// making object of TextWathcher class
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {// when text change in Edit tEXT

    }

    @Override
    public void afterTextChanged(Editable s) {
        checkFieldsForEmpty();// CHECK LOGIN BUTTON DISABLED AND ENABED
    }
};
/*This Broadcast receiver will listen network state accordingly
which enable or disable create an account button*/
private final BroadcastReceiver m_oOtpReceiver = new BroadcastReceiver() {// creating broadcast to receive otp sent by server from Inbox...
    @Override
    public void onReceive(Context context, Intent intent) {// on receive method to read OTP sent by server
        checkFieldsForEmpty();// check whether edit text is empty or not

    }
};/*This method check Edit text is empty or not*/
public void checkFieldsForEmpty() {// this method check Edit text is empty or not

    s_szMobileNumber = m_InputMobile.getText().toString().trim();// get mobile number from edit Text
    s_szPassword = m_InputPassword.getText().toString().trim();// get password from edit text

    if (NetworkUtil.isConnected(getApplicationContext())) {
        // if mobile number and password are Emoty
        if (s_szMobileNumber!= null && s_szMobileNumber.length()>7 && s_szMobileNumber.length()<15) {// check if mobile and password is empty ..
            if (s_szPassword.length()>=4&&s_szPassword.length()<=8) {
                m_LoginBtn.setEnabled(true);// make Login button disabled

                m_LoginBtn.setBackgroundColor(Color.rgb(0, 80, 147));// set background color on eabled
                m_LoginBtn.setOnClickListener(new View.OnClickListener() {// onclick listener on Login Button
                    @Override
                    public void onClick(View v) {
                        postLoginDataToServer();

                    }
                });
            }
            else {
                m_InputPassword.setError("Password must be between 4 to 8 characters long");
                m_LoginBtn.setEnabled(false);// make login button enabled
                m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192));// color of login button
            }

        } else {
            m_InputMobile.setError("Mobile number must be between 7 to 15 characters long");
            m_LoginBtn.setEnabled(false);// make login button enabled
            m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192));// color of login button

        }

    } else {
        try {
            CSnackBar.getInstance().showSnackBarError(findViewById(R.id.mainLayout), "No Internet Connection Available", getApplicationContext());

        } catch (Exception e) {
            e.printStackTrace();
        }
        m_LoginBtn.setEnabled(false);
        m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192));
    }


}

1 个答案:

答案 0 :(得分:0)

这是因为您SerialPort第一次调用BroadReceiver正在检查网络状态。并且第一次编辑文本没有值,因此长度为0,它将满足0不大于7的条件,因此它将首次显示错误。

解决方案:

如果从m_oOtpReceiver调用方法,则不必检查验证,因此跳过该部分。

BroadcastReceiver
来自textwatcher的

public void checkFieldsForEmpty(boolean fromBroadcast) {// this method check Edit text is empty or not

    s_szMobileNumber = m_InputMobile.getText().toString().trim();// get mobile number from edit Text
    s_szPassword = m_InputPassword.getText().toString().trim();// get password from edit text

    if (NetworkUtil.isConnected(getApplicationContext())) {

           if(!fromBroadcast){
                  // check validation
               }
           else{
                  // no need to check validation
             }
        }

来自广播接收器

@Override
    public void afterTextChanged(Editable s) {
        checkFieldsForEmpty(false);// CHECK LOGIN BUTTON DISABLED AND ENABED
    }

或者您可以检查是否显示错误消息。

 checkFieldsForEmpty(true);