英文:
EditText condition loop Android Studio
问题
我有一个EditText,我需要它至少包含一个字符。
如果没有,我会提示一个Toast,要求用户再试一次。
问题是循环进入无限循环,手机卡住了。
我假设这是一个简单的问题,但我不知道该怎么办。
我正在尝试从用户那里读取课程名称、成绩和分数。课程名称中至少需要有一个字符。可能是逻辑有问题。有什么提示吗?
public void readSubjectName() {
grades.add(new Grade()); // 创建新的成绩对象
String tempSubject = inputLabel.getText().toString();
/** 如果课程名称不包含任何字符,
* 认为它是无效的,弹出提示并重新要求输入。
*/
if (!(tempSubject.matches(".*[a-z].*"))) { // 课程名称不包含任何字符
Toast.makeText(getApplicationContext(), "请输入有效的课程名称", Toast.LENGTH_LONG).show();
while (!(tempSubject.matches(".*[a-z].*"))) {
Toast.makeText(getApplicationContext(), "请输入有效的课程名称", Toast.LENGTH_LONG).show();
}
}
grades.get(grades.size() - 1).setSubject(tempSubject); // 添加课程名称
inputLabel.setText(null);
inputLabel.setHint("输入成绩");
subjectButton.setVisibility(View.INVISIBLE);
gradeButton.setVisibility(View.VISIBLE);
}
英文:
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);
}
答案1
得分: 0
尝试这段代码:
public void readSubjectName() {
grades.add(new Grade()); // 创建新的成绩
String tempSubject = inputLabel.getText().toString();
if (tempSubject.isEmpty()) {
Toast.makeText(getApplicationContext(), "请输入有效的课程名称", Toast.LENGTH_LONG).show();
//inputLabel.setError("填写此字段"); TODO:检查这是否有效,否则显示 Toast 消息
inputLabel.requestFocus();
}
grades.get(grades.size() - 1).setSubject(tempSubject); // 添加科目
inputLabel.setText(null);
inputLabel.setHint("输入成绩");
subjectButton.setVisibility(View.GONE);
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...
答案2
得分: 0
使用以下代码,您可以评估用户输入的字符串/文本的长度,并检查它是否符合您的条件:
// 从 EditText 读取值到一个 String 变量
val tempSubject: String = inputLabel.text.toString()
// 检查 EditText 是否有值
if (tempSubject.trim().length > 1) {
Toast.makeText(applicationContext, "消息:$tempSubject", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(applicationContext, "请输入一些消息!", Toast.LENGTH_SHORT).show()
}
英文:
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()
}
答案3
得分: 0
我发现了问题所在,我需要保持当前状态,直到我获得正确的输入,只需使用简单的 "else" 语句:
// 检查 EditText 是否有值
if (!(tempSubject.matches(".*[a-z].*"))) {
inputLabel.setError("无效的名称!");
inputLabel.requestFocus();
} else {
grades.get(grades.size() - 1).setSubject(tempSubject); // 添加科目
inputLabel.setText(null);
inputLabel.setHint("输入成绩");
subjectButton.setVisibility(View.INVISIBLE);
gradeButton.setVisibility(View.VISIBLE);
}
我之前漏掉了 "else" 语句,所以代码的其余部分都在条件之外,因此代码只是继续执行。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论