EditText() 的奇怪问题 - Android Kotlin
Strange Problem with EditText() - Android Kotlin
我遇到了这个奇怪的问题:
// definition
var myEditText: EditText? = null
.
.
.
// instantiation
myEditText = EditText(this)
myEditText?.setSingleLine(true)
myEditText?.setTextColor(Color.YELLOW)
myEditText?.setBackgroundColor(Color.RED)
myEditText?.setPadding(10, 0, 10, 0)
myEditText?.gravity = Gravity.CENTER_VERTICAL
myEditText?.inputType = InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
myEditText?.setOnFocusChangeListener { view, hasFocus -> ...
rootView.addView(myEditText)
.
.
.
if (myEditText?.text == null || myEditText?.text.trim() == "") {
println("it's blank!") // doesn't execute
} else {
println(">>>" + myEditText?.text.trim() + "<<<")
}
// output: >>><<<
EditText() 控件已正确实例化并正确显示在屏幕上。我可以输入文本,它会 return 正确输入条目。
但是为什么不能识别文本框是空的?
myEditText?.text.toString()
你应该使用toString()
Returns a string representation of the object. In general, the
toString method returns a string that "textually represents" this
object. The result should be a concise but informative representation
that is easy for a person to read. It is recommended that all
subclasses override this method.
我遇到了这个奇怪的问题:
// definition
var myEditText: EditText? = null
.
.
.
// instantiation
myEditText = EditText(this)
myEditText?.setSingleLine(true)
myEditText?.setTextColor(Color.YELLOW)
myEditText?.setBackgroundColor(Color.RED)
myEditText?.setPadding(10, 0, 10, 0)
myEditText?.gravity = Gravity.CENTER_VERTICAL
myEditText?.inputType = InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
myEditText?.setOnFocusChangeListener { view, hasFocus -> ...
rootView.addView(myEditText)
.
.
.
if (myEditText?.text == null || myEditText?.text.trim() == "") {
println("it's blank!") // doesn't execute
} else {
println(">>>" + myEditText?.text.trim() + "<<<")
}
// output: >>><<<
EditText() 控件已正确实例化并正确显示在屏幕上。我可以输入文本,它会 return 正确输入条目。
但是为什么不能识别文本框是空的?
myEditText?.text.toString()
你应该使用toString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.