TextView.getText() 是否曾经 returns 为空?
Does TextView.getText() ever returns null?
类似于以下问题 Does EditText.getText() ever returns null?,但使用 TextView
而不是 EditText
。
如果视图的 XML
定义不包含 android:text
或者如果 android:text=""
?
,TextView.getText()
return 是否会为 null
NO.textView.getText()
永远不会returnnull
。根据定义 getText()
如下 .
public CharSequence getText() {
return mText;
}
你可以看到实例变量 mText
在 TextView
的构造函数中用 mText=""
初始化。您可以查看 TextView
的 source code。
public TextView(
Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mText = "";
.........
}
所以它将 return 文本和 ""
如果没有文本。
类似于以下问题 Does EditText.getText() ever returns null?,但使用 TextView
而不是 EditText
。
如果视图的 XML
定义不包含 android:text
或者如果 android:text=""
?
TextView.getText()
return 是否会为 null
NO.textView.getText()
永远不会returnnull
。根据定义 getText()
如下 .
public CharSequence getText() {
return mText;
}
你可以看到实例变量 mText
在 TextView
的构造函数中用 mText=""
初始化。您可以查看 TextView
的 source code。
public TextView(
Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mText = "";
.........
}
所以它将 return 文本和 ""
如果没有文本。