Does EditText.getText() ever returns null?

后端 未结 7 825
粉色の甜心
粉色の甜心 2020-11-30 09:31

All over the net I see examples like edittext.getText().toString(). I do not see any null check. In docs I do not see any statement that would say that this wil

相关标签:
7条回答
  • 2020-11-30 10:15

    With Android P SDK it is annotated as nullable in the AppCompatEditText class so it can return null.

    And from the docs:

    Return the text that the view is displaying. If an editable text has not been set yet, this will return null.

    @Override
    @Nullable
    public Editable getText() {
        if (Build.VERSION.SDK_INT >= 28) {
            return super.getText();
        }
        // A bug pre-P makes getText() crash if called before the first setText due to a cast, so
        // retrieve the editable text.
        return super.getEditableText();
    }
    
    0 讨论(0)
提交回复
热议问题