Show Error on the tip of the Edit Text Android

前端 未结 12 2025
一生所求
一生所求 2020-12-01 04:30

I want to show error if the user enters blank value in the edittext.But i am not getting the way how could i do this .This is how i want like this:

相关标签:
12条回答
  • 2020-12-01 04:41
    if(TextUtils.isEmpty(firstName.getText().toString()){
          firstName.setError("TEXT ERROR HERE");
    }
    

    Or you can also use TextInputLayout which has some useful method and some user friendly animation

    0 讨论(0)
  • 2020-12-01 04:43

    I know it's too late, but in case someone still need help. Here is the working solution. Setting an error is pretty straight forward. But it will be displayed to user, when he request Focus on it. So to do the both thing on your own, User this code.

     firstName.setError("Enter FirstName");
     firstName.requestFocus();
    
    0 讨论(0)
  • 2020-12-01 04:43

    u can use this :

    @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if (s.length() == Bank.PAN_MINIMUM_RECOGNIZABLE_LENGTH + 10) {
                    Bank bank = BankUtil.findByPan(s.toString());
                    if (null != bank && mNewPanEntered && !mNameDefined) {
                        mNewPanEntered = false;
                        suggestCardName(bank);
                    }
    

    private void suggestCardName(Bank bank) {
        mLastSuggestTime = System.currentTimeMillis();
        if (!bank.getName().trim().matches(getActivity().getString(R.string.bank_eghtesadnovin))) {
            inputCardNumber.setError(R.string.balance_not_enmb, true);
    
        }
    }
    
    0 讨论(0)
  • 2020-12-01 04:44

    You can show error as PopUp of EditText

    if (editText.getText().toString().trim().equalsIgnoreCase("")) {
          editText.setError("This field can not be blank");
    }
    

    and that will be look a like as follows

    enter image description here

    firstName.addTextChangedListener(new TextWatcher()  {
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        @Override
        public void afterTextChanged(Editable s)  {
            if (firstName.getText().toString().length <= 0) {
                firstName.setError("Enter FirstName");
            } else {
                firstName.setError(null);
            }
        }
     });
    
    0 讨论(0)
  • 2020-12-01 04:46

    you could use an onchange event to trigger a validation and then you can display a toast if this is enough for you

    0 讨论(0)
  • 2020-12-01 04:50
    private void showError() {
       mEditText.setError("Password and username didn't match");
    }
    

    Which will result in errors shown like this:

    enter image description here

    And if you want to remove it:

     textView.setError(null);
    
    0 讨论(0)
提交回复
热议问题