清除editText randomNumber

时间:2019-06-14 13:16:16

标签: java android

我想在每次尝试后将数字编辑文本字段重置为空。

    EditText editText = (EditText) findViewById(R.id.editText);
    int guessValue = Integer.parseInt(editText.getText().toString());
    String message;

    if (guessValue > randomNumber) {
        message = "Lower !";
    } else if (guessValue < randomNumber) {
        message = "Higher";
    } else {
        message = "You got it! Try again";
        generateRandomNumber();
    }

2 个答案:

答案 0 :(得分:0)

您可以使用.getText().clear()方法清除文本, 我在示例下方发布了该示例,该示例可以帮助您了解如何清除文本框。

((EditText) findViewById(R.id.editText)).getText().clear();

适用于您的代码

    EditText editText = (EditText) findViewById(R.id.editText); 
    int guessValue = Integer.parseInt(editText.getText().toString()); 
    editText.getText().clear(); 
    String message; 

    if (guessValue > randomNumber) { 
        message = "Lower !"; 
    } 
    else if (guessValue < randomNumber) { 
        message = "Higher"; 
    } 
    else { 
        message = "You got it ! Try again"; generateRandomNumber(); 
    }

答案 1 :(得分:0)

EditText editText = (EditText) findViewById(R.id.editText);
int guessValue = Integer.parseInt(editText.getText().toString());
editText.getText.clear();  //or editText.setText("");
String message;

if (guessValue > randomNumber) {
    message = "Lower !";
} else if (guessValue < randomNumber) {
    message = "Higher";
} else {
    message = "You got it! Try again";
    generateRandomNumber();
}