如何在Android Studio中使字符计数文本视图在编辑文本为空时保持空白。

huangapple go评论49阅读模式
英文:

How to keep the Character Count Text View Blank when an Edit text is empty in Android Studio

问题

已经弄清楚了如何在用户在EditText中输入时显示单词计数,但现在当文本字段中没有字符时,我希望字符计数为空。

这是我所说的内容的示例图片。

如何在Android Studio中使字符计数文本视图在编辑文本为空时保持空白。

以下是将单词计数与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.

如何在Android Studio中使字符计数文本视图在编辑文本为空时保持空白。

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.GONEView.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)

huangapple
  • 本文由 发表于 2023年3月4日 02:15:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定