kotlin 如何在Android Studio中返回页面时使字符数不可见

igetnqfo  于 2023-03-13  发布在  Kotlin
关注(0)|答案(1)|浏览(169)

所以我把它设置为当相应的EditText框处于焦点时显示字符计数。并且当用户单击其他地方时不可见。但是当我进入不同的页面,然后回到上一页时,所有的字符计数字段都会弹出,但是一旦你点击每个单独的字段,它就会恢复功能。所以我希望那些字符字段保持非从其他页面返回到该页面时可见。
下面是控制字符计数TextView的代码。

/** Having the Character Count Box observe the state of the corresponding EditText Box*/
            itemView.date_time_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
                if (!hasFocus) itemView.cCount1.visibility = View.GONE
                else itemView.cCount1.visibility = View.VISIBLE
            }
            itemView.cable_run_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
                if (!hasFocus) itemView.cCount2.visibility = View.GONE
                else itemView.cCount2.visibility = View.VISIBLE
            }
            itemView.entrance_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
                if (!hasFocus) itemView.cCount3.visibility = View.GONE
                else itemView.cCount3.visibility = View.VISIBLE
            }
            itemView.door_lock_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
                if (!hasFocus) itemView.cCount4.visibility = View.GONE
                else itemView.cCount4.visibility = View.VISIBLE
            }
            itemView.handicap_operator_add.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
                if (!hasFocus) itemView.cCount5.visibility = View.GONE
                else itemView.cCount5.visibility = View.VISIBLE
            }

            /** A custom character count feature with an EditText and TextView */
            itemView.date_time_add.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
                    itemView.cCount1.visibility = View.GONE
                }

                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
                    }
                }

                override fun afterTextChanged(s: Editable?) {
                    if (!s.isNullOrEmpty()) {
                        itemView.cCount1.text = "${255 - s.length} Characters Remaining"
                    }
                    else {
                        itemView.cCount1.visibility = View.GONE
                    }
                }

            })

            itemView.cable_run_add.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
                    itemView.cCount2.text = ""
                }

                override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
                    if (s.isNullOrEmpty()) {
                        itemView.cCount2.visibility = View.GONE
                    } else {
                        itemView.cCount2.visibility = View.VISIBLE
                    }
                }

                override fun afterTextChanged(s: Editable?) {
                    if (!s.isNullOrEmpty()) {
                        itemView.cCount2.text = "${255 - s.length} Characters Remaining"
                    }
                    else{
                        itemView.cCount2.visibility = View.GONE
                    }
                }

            })

            itemView.entrance_add.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
                    itemView.cCount3.text = ""
                }

                override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
                    if (s.isNullOrEmpty()) {
                        itemView.cCount3.visibility = View.GONE
                    } else {
                        itemView.cCount3.visibility = View.VISIBLE
                    }
                }

                override fun afterTextChanged(s: Editable?) {
                    if (!s.isNullOrEmpty()) {
                        itemView.cCount3.text = "${255 - s.length} Characters Remaining"
                    }
                    else {
                        itemView.cCount3.visibility = View.GONE
                    }
                }

            })

            itemView.door_lock_add.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
                    itemView.cCount4.text = ""
                }

                override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
                    if (s.isNullOrEmpty()) {
                        itemView.cCount4.visibility = View.GONE
                    } else {
                        itemView.cCount4.visibility = View.VISIBLE
                    }
                }

                override fun afterTextChanged(s: Editable?) {
                    if (!s.isNullOrEmpty()) {
                        itemView.cCount4.text = "${1200 - s.length} Characters Remaining"
                    }
                    else {
                        itemView.cCount4.visibility = View.GONE
                    }
                }

            })

            itemView.handicap_operator_add.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
                    itemView.cCount5.text = ""
                }

                override fun onTextChanged(s: CharSequence?, start: Int, count: Int, end: Int) {
                    if (s.isNullOrEmpty()) {
                        itemView.cCount5.visibility = View.GONE
                    } else {
                        itemView.cCount5.visibility = View.VISIBLE
                    }
                }

                override fun afterTextChanged(s: Editable?) {
                    if (!s.isNullOrEmpty()) {
                        itemView.cCount5.text = "${1200 - s.length} Characters Remaining"
                    }
                    else {
                        itemView.cCount5.visibility = View.GONE
                    }
                }

            })

        }
    }

澄清:我希望在返回到包含字符计数的片段时使字符计数TextView不可见。例如,我正在填写一个包含字符计数的表单,我转到另一个片段,然后返回到包含该表单的片段,此时字符计数可见。我希望在返回到该片段时使字符计数TextView不可见。

  • 当用户返回到该片段时使字符计数不可见。

任何建议都很感谢。谢谢。

blpfk2vs

blpfk2vs1#

不确定是否理解得很好。您想要实现的是,当您“进入不同的页面”(您是指Activity吗?)时,您希望新页面中出现一些特殊行为?如果是这样,我假设您正在为此使用Intent。然后,您可以简单地在打开的Activity中添加额外的String,检查该String并执行一些操作。
示例:

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", "exampleValue"); //Optional parameters
CurrentActivity.this.startActivity(myIntent);

然后在NextActivity中,您可以获得如下所示的“key”值:

Intent intent = getIntent();
String value = intent.getStringExtra("key"); //if it's a string you stored

if (value.equals("exampleValue")
// do some work

希望能有所帮助!

相关问题