How to only allow positive numbers in an EditText
You should use the attr of EditText:
android:digits="0123456789."
android:inputType="number"
http://developer.android.com/reference/android/widget/TextView.html#attr_android:digits
So user will be able enter only digits without minus.
UPDATED: Something like that, to prevent first sign from 0:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable edt) {
if (edt.length() == 1 && edt.toString().equals("0"))
editText.setText("");
}
// ...
});
For each EditText
, you have to do this :
if (Integer.parseInt(et1local.getText().toString()) > 0)
Do a function to ensure it's a number
private boolean isPositive(EditText et)
{
try
{
return Integer.parseInt(et.getText().toString()) > 0;
}
catch (Exception e)
{
return false;
}
}
Use it like that :
if (et1local.getText().toString().length() == 0 || !isPositive(et1local)
|| et2local.getText().toString().length() == 0 || !isPositive(et2local)
// [...]