EditText

  1. 监听EditText字符长度,作出响应。比如隐藏软键盘
//软键盘工具类 https://github.com/Blankj/AndroidUtilCode
custom_login_verify.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
    @Override
    public void afterTextChanged(Editable s) {
       if (custom_login_verify.getText().toString().trim().length() == 4){
           if (KeyboardUtils.isSoftInputVisible(LoginActivity.this)) {
               KeyboardUtils.hideSoftInput(custom_login_verify);
           }
       }
    }
});
  1. 监听EditText获取焦点状态,作出响应。比如改变下划线的颜色

    custom_login_verify.setOnFocusChangeListener(this);
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.custom_phone_num:
                if (hasFocus) {
                    phoneDrawbleLeft = getResources().getDrawable(R.drawable.icon_shouji_selected);
                    view_phone.setBackgroundColor(getResources().getColor(R.color.line_tab));
                } else {
                    phoneDrawbleLeft = getResources().getDrawable(R.drawable.icon_shouji);
                    view_phone.setBackgroundColor(getResources().getColor(R.color.text_normal_color));
                }
                custom_phone_num.setCompoundDrawablePadding(ConvertUtils.dp2px(30));
                phoneDrawbleLeft.setBounds(0, 0, phoneDrawbleLeft.getMinimumWidth(), phoneDrawbleLeft.getMinimumHeight());
                custom_phone_num.setCompoundDrawables(phoneDrawbleLeft, null, null, null);
                break;      
        }
    }
    

你可能感兴趣的:(Android)