验证EditText输入并更新用户分数

时间:2017-02-18 03:34:44

标签: java android-studio

案例:
我问用户一个问题,答案是7

如何验证EditText的条目是否为7,之后我想奖励1分。

//Question #1  
//If Answer is equal to 7 than award One Point   
int = score;

private EditText questionOneAnswer;

EditText text = (EditText) findViewById(R.id.Part2Question1);

String EditText = text.getText().toString().trim();

if(EditText.isEmpty() || EditText.length() == 0 || 
  EditText.equals("7") || EditText == null)
   {
    //EditText is empty
}

1 个答案:

答案 0 :(得分:0)

首先,您不能将预定义的类名用作变量名。其次是你对授予点的逻辑是不正确的。试试这个,

EditText text = (EditText) findViewById(R.id.Part2Question1);
String answer = text.getText().trim();

if(answer.equals("7"))
   {
    score++;
   }
else{
     // show some toast showing that answer is incorrect
   }