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);
}
});
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.
– Leonardo Saldanha
@Leonardosaldanha you want when having at least one number it changes the state of a button?
– Math
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?
– Leonardo Saldanha
@Leonardosaldanha you are covered by reason, rs
– Math
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.
– Leonardo Saldanha
Yeah, @Math! I’ve already deleted the answer :)
– carlosrafaelgn
@carlosrafaelgn could have tried to fix, just warned not to win votes against silly
– Math
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 ;)
– carlosrafaelgn
@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.
– Leonardo Saldanha
@Leonardosaldanha is vdd, the button has to start disabled from the beginning, I will fix in my code
– Math
So as a solution, I chose to put the initial button configuration as disabled.
– Leonardo Saldanha
Exactly! hahahaha
– Leonardo Saldanha
It worked fine, thanks!
– Leonardo Saldanha
@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.
– Leonardo Saldanha