TextView.getText()是否会返回null?

时间:2018-03-29 16:33:56

标签: android android-edittext textview

与以下问题类似Does EditText.getText() ever returns null?,但$newArr = { array("id" => 1, "key" => 1, "content" => "blah", "extra" => "extra data"), array("id" => 2, "key" => 2, "content" => "bleep", "extra" => "extra data"), array("id" => 3, "key" => 3, "content" => "bloop", "extra" => "extra data"), array("id" => 4, "key" => 2, "content" => "zip", "extra" => "extra data"), array("id" => 5, "key" => 1, "content" => "zorp", "extra" => "extra data"), array("id" => 6, "otherkey" => 1, "title" => "foo"), array("id" => 7, "otherkey" => 2, "title" => "bar"), array("id" => 8, "otherkey" => 3, "title" => "baz") }; 代替TextView

如果视图的EditText定义不包含TextView.getText()XML,那么android:text会返回null吗?

1 个答案:

答案 0 :(得分:3)

NO。 textView.getText()永远不会返回null。根据定义,getText()如下。

public CharSequence getText() {
    return mText;
}

您可以在mText的构造函数中看到使用mText=""初始化的实例变量TextView。您可以查看TextView的{​​{3}}。

public TextView(
    Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    mText = "";
    ......... 
}

因此,如果没有文字,它将返回文本和""