I have an EditText that I need to contain at least one character. In case it doesn't, I prompt a Toast and ask the user to try again. The problem is that the loop goes infinity \ the phone is stuck. I assume that it's a simple question, yet I cant figure out what to do. I'm trying to read from user the Course name, grade, and points. I need to have at least one char in the course name. Probably something wrong with the logic. Any hints?
public void readSubjectName() {
grades.add(new Grade()); // Create new grade
String tempSubject = inputLabel.getText().toString();
/** If the Subject name does not contain any characters,
* assume it's bad, toast and ask again.
*/
if (!(tempSubject.matches(".*[a-z].*"))) { // Subject Name does not contain any characters
Toast.makeText(getApplicationContext(), "Please enter a valid course name", Toast.LENGTH_LONG).show();
while (!(tempSubject.matches(".*[a-z].*"))) {
Toast.makeText(getApplicationContext(), "Please enter a valid course name", Toast.LENGTH_LONG).show();
}
}
grades.get(grades.size() - 1).setSubject(tempSubject); //Add subject
inputLabel.setText(null);
inputLabel.setHint("Enter Grade");
subjectButton.setVisibility(View.INVISIBLE);
gradeButton.setVisibility(View.VISIBLE);
}
Try this code
public void readSubjectName() {
grades.add(new Grade()); // Create new grade
String tempSubject = inputLabel.getText().toString();
if (tempSubject.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter a valid course name", Toast.LENGTH_LONG).show();
//inputLabel.setError("Fill this field"); TODO: Check whether this works else display Toast Message
inputLabel.requestFocus();
}
grades.get(grades.size() - 1).setSubject(tempSubject); //Add subject
inputLabel.setText(null);
inputLabel.setHint("Enter Grade");
subjectButton.setVisibility(View.GONE);
gradeButton.setVisibility(View.VISIBLE);
}
Hope this helps. Feel free to ask for clarifications...
With below code, you can evaluate the length of the string/text entered by the user and check if it matches your criterion,
//read value from EditText to a String variable
val tempSubject: String = inputLabel.text.toString()
//check if the EditText have values or not
if(tempSubject.trim().length>1) {
Toast.makeText(applicationContext, "Message : "+tempSubject, Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(applicationContext, "Please enter some message! ", Toast.LENGTH_SHORT).show()
}
So I figured out what was mistake, I needed to keep the current state and not move to the next state until I get a proper input, using simple the "else" statement:
//check if the EditText have values or not
if (!(tempSubject.matches(".*[a-z].*"))) {
inputLabel.setError("Invalid name!");
inputLabel.requestFocus();
} else {
grades.get(grades.size() - 1).setSubject(tempSubject); //Add subject
inputLabel.setText(null);
inputLabel.setHint("Enter Grade");
subjectButton.setVisibility(View.INVISIBLE);
gradeButton.setVisibility(View.VISIBLE);
}
I was missing the else statement, the rest of the code was out of the condition so the code just continued regardless.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.