英文:
How to keep the Character Count Text View Blank when an Edit text is empty in Android Studio
问题
已经弄清楚了如何在用户在EditText中输入时显示单词计数,但现在当文本字段中没有字符时,我希望字符计数为空。
这是我所说的内容的示例图片。
以下是将单词计数与EditText连接的代码。
itemView.date_time_add.addTextChangedListener(object: TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
itemView.cCount1.text = ""
}
override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
if (s.isNullOrEmpty()) {
itemView.cCount1.text = ""
}
}
override fun afterTextChanged(s: Editable?) {
if (s != null) {
itemView.cCount1.text = "${225 - s.length} Characters Remaining"
}
}
})
欢迎任何想法,谢谢。
英文:
So I already figured out how to have the word count show during the user typing inside the EditText, but Now I want the character count to be blank when there is no characters in the text field.
Here's a pick of what I am talking about.
And here's the code that is connecting the word count to the Edit Text
itemView.date_time_add.addTextChangedListener(object: TextWatcher{
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
itemView.cCount1.text = ""
}
override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
if (s.isNullOrEmpty()){
itemView.cCount1.text = ""
}
}
override fun afterTextChanged(s: Editable?) {
if (s != null) {
itemView.cCount1.text = "${225 - s.length} Characters Remaining"
}
}
} )
Any ideas is welcomed, thank you.
答案1
得分: 2
要隐藏或显示视图,您可以使用.visibility
属性。要显示,您可以使用View.VISIBLE
,要隐藏则使用View.GONE
或View.INVISIBLE
。
override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
if (s.isNullOrEmpty()){
itemView.cCount1.visibility = View.GONE
} else {
itemView.cCount1.visibility = View.VISIBLE
}
}
更多详细信息请参考:https://developer.android.com/reference/android/view/View#setVisibility(int)
英文:
To hide or show the View you can use .visibility
property. To show you can use View.VISIBLE
, and to hide View.GONE
or View.INVISIBLE
override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
if (s.isNullOrEmpty()){
itemView.cCount1.visibility = View.GONE
} else {
itemView.cCount1.visibility = View.VISIBLE
}
}
Reference for more details: https://developer.android.com/reference/android/view/View#setVisibility(int)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论