2017-02-18 46 views
0

案例:
我在問用戶問題的答案是7驗證EditText輸入並更新用戶分數

我如何驗證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 
} 

回答

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 
    }