سؤال

I want to add validation to the input of my EditText so that if a user enters a number outside of 1-12 the application does not move to the next Activity, instead I want it to just do nothing and allow the user to re-enter a valid number. I do not know how to do so.

Current code of EditText Section:

//Setting up next activity to open  via intent
Intent intent = new Intent(this, PracticeTest.class);
//Validation to ensure number is between 1 and 12
if (convertedNumber2 >= 1 && convertedNumber2 <= 12) {
    //pass int value
    intent.putExtra("convertedNumber2", convertedNumber2);

} else {

}
//open activity
startActivity(intent);
هل كانت مفيدة؟

المحلول

        //Validation to ensure number is between 1 and 12
        if (convertedNumber2 >= 1 && convertedNumber2 <= 12) {
             //Setting up next activity to open via intent
             Intent intent = new Intent(this, PracticeTest.class);

            //pass int value
            intent.putExtra("convertedNumber2", convertedNumber2);

            //open activity
            startActivity(intent);
        } else {
            Toast.makeText(this, "Opss, wrong value!", Toast.LENGTH_SHORT).show(); 
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top