Set at least one number in Edittext

Asked

Viewed 1,112 times

1

My question is this::

Is there any way to prevent the click of a button if an Edittext has no number at all? I was able to limit minimum and maximum values (0 to 255), but I could not find a solution to this problem. Because "" is not an integer value, by clicking the button without filling the three fields with some number, the app crasha.

I tried to use this medium, but I was unsuccessful:

btnCalc.setOnClickListener (new View.OnClickListener()
            {
                public void onClick(View v)
                {   
                    if (edtRed == null || edtGreen == null || edtBlue == null)
                    {
                        btnCalc.setEnabled(false);
                    }
                    else
                    {
                        btnCalc.setEnabled(true);
                        getCode();
                    }
                    if (n1 > 255 || n1 < 0 || n2 > 255 || n2 < 0 || n3 > 255 || n3 < 0)
                    {
                        result = "#FFFFFF";
                        txtResult.setText(result);
                    }
                    else
                    {           
                        Calculate();
                    }   
                    result = "#" + rst1 + remainderR + rst2 + remainderG + rst3 + remainderB;
                    txtResult.setText(result);
                }               
            });

1 answer

2


Test the size of the Edittext content in the Editor which indicates that there has been a change in it, if the content size of your Edittext is greater than zero enable the button, otherwise disable.

As the author of the question observed himself, the button should be initialized already disabled, because Edittext starts empty. It can be disabled in both xml and code.

Example:

btnOk = (EditText)findViewById(R.id.btnOk);
btnOk.setEnabled(false); //já inicia desabilitado
txtQuantidade = (EditText)findViewById(R.id.txtQuantidade);
txtQuantidade.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        if(txtQuantidade.length() > 0 ) { //verifica tamanho do conteúdo do EditText
            btnOk.setEnabled(true);       //habilita botão
        }
        else {
            btnOk.setEnabled(false);      //desabilita botão
        }
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
}); 

Your content will always be a number, because as you said yourself your Edittext accepts only numbers:

<EditText
    android:id="@+id/txtQuantidade"
    android:inputType="number" //aceita apenas números
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:ems="10" >
</EditText>
  • I’ve already done that. My question is not about limiting only numbers, but rather limiting a minimum number of digits before the button is "enabled", since the int variable that receives the Edittext value does not accept it as empty.

  • @Leonardosaldanha you want when having at least one number it changes the state of a button?

  • There’s no way it’s not a number. I defined inputType, so the right one would be to only receive numbers, it’s not?

  • 1

    @Leonardosaldanha you are covered by reason, rs

  • So, I just needed to find a way of the variable that receives these values, see "" as a number that exceeds the 255 limit, or just didn’t accept, if any of the three Edittext were empty.

  • Yeah, @Math! I’ve already deleted the answer :)

  • @carlosrafaelgn could have tried to fix, just warned not to win votes against silly

  • Thanks for the touch, @Math! But I’m half working/half looking here. When I went to see, it was late, you had already answered ;)

  • @Math , I tested this code and found an exception. If the text is not changed, the button will remain enabled until something is entered. That is, if someone simply initializes the application and clicks on the button, the crash will happen as if they did not have this excerpt.

  • 1

    @Leonardosaldanha is vdd, the button has to start disabled from the beginning, I will fix in my code

  • So as a solution, I chose to put the initial button configuration as disabled.

  • Exactly! hahahaha

  • It worked fine, thanks!

  • @Math, there’s one detail I just realized. In the case of more than one Edittext, if only one is filled, the button is enabled in the same way, so you need to use an "E" function after "txtQuantidity.length() > 0" to add a condition to another Edit.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.