如何在android中输入文本时检查编辑文本的值?

时间:2016-06-07 09:56:39

标签: android android-edittext

我有一个名为手机和密码的编辑文本字段,方案是当应用启动它时出现错误文本,在相应字段中输入文本时我想要的内容,在完成编辑文本后检查值是否正确然后移动其他在该字段中显示错误。

代码: -

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(true);// check whether edit text is empty or not

    }
};
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(false);// CHECK LOGIN BUTTON DISABLED AND ENABED
    }
};

/*This method check Edit text is empty or not*/
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 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 {
                    if (!fromBroadcast) {

                        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 {
                if (!fromBroadcast) {
                    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)

我无法看到boolean“fromBroadcast”的初始化位置,或者在显示错误消息之前知道是否需要检查它的状态。根据我的阅读,我认为你不需要使用if语句来检查“fromBroadcast”。代码应该没有它。如果您正在使用错误消息启动应用程序并且您不想要错误消息,那么您应该检查s_szPassword而不是m_InputPassword以进行输入,并且您可以初始化s_szPassword以包含正确的单词,因此错误消息msg在开始时不存在。