如何从编辑文本中松开焦点按键盘回到android?

时间:2016-06-23 04:42:59

标签: android

我有两个编辑文本,即手机和密码,当活动可见时,移动编辑文本可聚焦,同时完成输入手机号码我想检查验证并按下键盘返回键盘关闭并希望专注于密码编辑文本并使用密码执行相同的过程。我可以这样做。 代码: -

private void init() {
    m_MainLayout = (LinearLayout) findViewById(R.id.mainLayout);
    /*check whether SoftKeyPad open or hide*/
    m_MainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = m_MainLayout.getRootView().getHeight() - m_MainLayout.getHeight();
            if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...

            } else {/*if hide then clear focus from Password Edit text*/
                m_InputPassword.clearFocus();
                checkFieldsForEmpty();
            }
        }
    });
    m_LoginBtn = (AppCompatButton) findViewById(R.id.btn_Login);// finding Id login botton

    m_InputMobile = (EditText) findViewById(R.id.input_mobile);// finding Id of Mobile Number edit text
    /*set focus change listener on Mobile Number Edit text*/
    m_InputMobile.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            /*if mobile Edit text looses focus then....*/
            if (!hasFocus) {
                isValidMobile();// initalize method
            }
        }
    });
    /* set Editor action listener on Mobile number Edit text*/
    m_InputMobile.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            /*if after input enter next keyWord*/
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                m_InputPassword.requestFocus();// then Request Password Edi text field to focus
            }
            return false;
        }
    });
    m_InputPassword = (EditText) findViewById(R.id.input_password);// finding Id of assword editText
    m_InputPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);// defining password edit Tect Input type
    m_InputPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
    /*set Foucs change listener on password Edit text*/
    m_InputPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            /*if password field loose focus*/
            if (!hasFocus) {

                isValidPassword();

            }

        }
    });
    /*Adding Editor action listener*/
    m_InputPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT) {
                m_InputPassword.clearFocus();
                checkFieldsForEmpty();
            }
            return false;
        }
    });

}

/*validation for mobile number*/
private void isValidMobile() {// validation for mobile number
    s_szMobileNumber = m_InputMobile.getText().toString().trim();// get mobile number from edit text
    if (s_szMobileNumber.length() > 7 && s_szMobileNumber.length() < 15) {
        checkFieldsForEmpty();
    } else {
        m_InputMobile.setError("Mobile number must be between 7 to 15 characters long");
    }

}

/*validation for password*/
private void isValidPassword() {// validation for password
    s_szPassword = m_InputPassword.getText().toString().trim();// get password from edit text
    if (s_szPassword.length() >= 4 && s_szPassword.length() <= 8) {
        checkFieldsForEmpty();
    } else {
        m_InputPassword.setError("Password must be between 4 to 8 characters long");
    }
}

/*This method check Edit text is empty or not*/
private 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();
    if (NetworkUtil.isConnected(getApplicationContext())) {
        // if mobile number and password are Emoty
        if (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_LoginBtn.setEnabled(false);// make login button enabled
                m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192));// color of login button

            }

        } else {
            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));
    }


}

0 个答案:

没有答案
相关问题