String string = editText.getText()。toString()给我string = null

时间:2015-09-18 16:50:21

标签: string android-studio android-edittext gettext toast

我使用EditText,我需要从EditText的输入中获取一个String。 但每次我使用final EditText editText = (EditText) findViewByID(editText); String string = editText.getText().toString()并将其放入Toast Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();时,我会得到一个空的Toast(null)。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

        @Override
        public void afterTextChanged(Editable arg0) {
        String string = editText.getText().toString();
        Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();

        }
    });