Android Studio返回页面时如何隐藏字符计数TextView控件
问题描述
我给对应的EditText设置了焦点监听:获取焦点时显示字符计数TextView,失去焦点时隐藏该控件。但切换到其他Fragment再返回当前页面时,所有字符计数控件都会自动显示,点击单个控件后才恢复正常逻辑。我需要返回该Fragment时,字符计数TextView保持隐藏状态。
解决方案
问题根源是Fragment切换回前台时,视图状态未被正确重置。我们需要主动同步EditText的焦点、文本状态与计数控件的可见性,具体处理如下:
- 初始化默认隐藏:视图创建阶段直接将所有计数TextView设为隐藏,避免初始化时默认显示。
- TextWatcher加焦点判断:仅当EditText处于获焦状态时,才根据文本内容更新计数控件可见性,防止无焦点时文本变化导致控件误显示。
- 重写onResume方法:Fragment回到前台时,主动遍历所有EditText,根据当前状态重置计数控件的可见性。
- 封装通用方法:减少重复代码,统一处理EditText与计数控件的状态同步逻辑。
修改后的代码示例
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // 初始化时默认隐藏所有计数控件 itemView.cCount1.visibility = View.GONE itemView.cCount2.visibility = View.GONE itemView.cCount3.visibility = View.GONE itemView.cCount4.visibility = View.GONE itemView.cCount5.visibility = View.GONE /** 焦点状态监听 */ itemView.date_time_add.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus -> itemView.cCount1.visibility = if (hasFocus) View.VISIBLE else View.GONE // 获焦且文本非空时更新计数文本 if (hasFocus && !itemView.date_time_add.text.isNullOrEmpty()) { itemView.cCount1.text = "${255 - itemView.date_time_add.text.length} 剩余字符" } } itemView.cable_run_add.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus -> itemView.cCount2.visibility = if (hasFocus) View.VISIBLE else View.GONE if (hasFocus && !itemView.cable_run_add.text.isNullOrEmpty()) { itemView.cCount2.text = "${255 - itemView.cable_run_add.text.length} 剩余字符" } } itemView.entrance_add.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus -> itemView.cCount3.visibility = if (hasFocus) View.VISIBLE else View.GONE if (hasFocus && !itemView.entrance_add.text.isNullOrEmpty()) { itemView.cCount3.text = "${255 - itemView.entrance_add.text.length} 剩余字符" } } itemView.door_lock_add.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus -> itemView.cCount4.visibility = if (hasFocus) View.VISIBLE else View.GONE if (hasFocus && !itemView.door_lock_add.text.isNullOrEmpty()) { itemView.cCount4.text = "${1200 - itemView.door_lock_add.text.length} 剩余字符" } } itemView.handicap_operator_add.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus -> itemView.cCount5.visibility = if (hasFocus) View.VISIBLE else View.GONE if (hasFocus && !itemView.handicap_operator_add.text.isNullOrEmpty()) { itemView.cCount5.text = "${1200 - itemView.handicap_operator_add.text.length} 剩余字符" } } /** 文本变化监听(增加焦点判断) */ itemView.date_time_add.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {} override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) { if (itemView.date_time_add.hasFocus()) { itemView.cCount1.visibility = if (s.isNullOrEmpty()) View.GONE else View.VISIBLE } } override fun afterTextChanged(s: Editable?) { if (!s.isNullOrEmpty() && itemView.date_time_add.hasFocus()) { itemView.cCount1.text = "${255 - s.length} 剩余字符" } } }) itemView.cable_run_add.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {} override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) { if (itemView.cable_run_add.hasFocus()) { itemView.cCount2.visibility = if (s.isNullOrEmpty()) View.GONE else View.VISIBLE } } override fun afterTextChanged(s: Editable?) { if (!s.isNullOrEmpty() && itemView.cable_run_add.hasFocus()) { itemView.cCount2.text = "${255 - s.length} 剩余字符" } } }) itemView.entrance_add.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {} override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) { if (itemView.entrance_add.hasFocus()) { itemView.cCount3.visibility = if (s.isNullOrEmpty()) View.GONE else View.VISIBLE } } override fun afterTextChanged(s: Editable?) { if (!s.isNullOrEmpty() && itemView.entrance_add.hasFocus()) { itemView.cCount3.text = "${255 - s.length} 剩余字符" } } }) itemView.door_lock_add.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {} override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) { if (itemView.door_lock_add.hasFocus()) { itemView.cCount4.visibility = if (s.isNullOrEmpty()) View.GONE else View.VISIBLE } } override fun afterTextChanged(s: Editable?) { if (!s.isNullOrEmpty() && itemView.door_lock_add.hasFocus()) { itemView.cCount4.text = "${1200 - s.length} 剩余字符" } } }) itemView.handicap_operator_add.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {} override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) { if (itemView.handicap_operator_add.hasFocus()) { itemView.cCount5.visibility = if (s.isNullOrEmpty()) View.GONE else View.VISIBLE } } override fun afterTextChanged(s: Editable?) { if (!s.isNullOrEmpty() && itemView.handicap_operator_add.hasFocus()) { itemView.cCount5.text = "${1200 - s.length} 剩余字符" } } }) } // Fragment回到前台时重置控件状态 override fun onResume() { super.onResume() updateCountView(itemView.date_time_add, itemView.cCount1, 255) updateCountView(itemView.cable_run_add, itemView.cCount2, 255) updateCountView(itemView.entrance_add, itemView.cCount3, 255) updateCountView(itemView.door_lock_add, itemView.cCount4, 1200) updateCountView(itemView.handicap_operator_add, itemView.cCount5, 1200) } // 封装通用方法,同步EditText和计数控件状态 private fun updateCountView(editText: EditText, countView: TextView, maxLength: Int) { countView.visibility = if (editText.hasFocus() && !editText.text.isNullOrEmpty()) { countView.text = "${maxLength - editText.text.length} 剩余字符" View.VISIBLE } else { View.GONE } }
内容的提问来源于stack exchange,提问作者OEThe11
相关产品推荐
相关产品推荐

