Получение ввода от Editext - PullRequest
       12

Получение ввода от Editext

0 голосов
/ 01 ноября 2019

Я пытаюсь получить ввод из поля EditText в Android и использовать его в другом методе, но ничего не происходит. Я не уверен, что ошибка в getInput или в методе, который ее вызывает. Вот методы:

public String getUserInput() {
    EditText userInput = findViewById(R.id.inputAnswer);
    return userInput.getText().toString();
}

public void checkQuestion3() {
    String name = getUserInput();
    if (name.trim().equalsIgnoreCase("Class")) {
        score += 1;
    }
}

и текст редактирования в xml:

<EditText
    android:id="@+id/inputAnswer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Your Answer"
    android:inputType="text"
    android:maxLength="15" />

1 Ответ

0 голосов
/ 01 ноября 2019

Вам нужно добавить слушателя или какую-то кнопку для вызова метода.

userInput.addTextChangedListener(new TextWatcher() {

    public void afterTextChanged(Editable s) {
        // you can call or do what you want with your EditText here
        // yourEditText... 
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
...