Android自定义编辑文本值被另一个自定义编辑文本更改

米哈尔·兹拉登克(Michal Zhradnk Nono)3551

介绍

在我的一个项目中,我尝试创建带有标题和一些自定义验证的自定义EditText。当我通过屏幕旋转和活动娱乐功能测试此自定义视图时,遇到了一个奇怪的问题。

有什么问题

娱乐之前

当应用启动时,所有编辑文本均具有从活动静态设置的正确值。如下图所示:

在此处输入图片说明

娱乐后

旋转屏幕或重新创建活动后,EditText的值将被弄乱。CustomEditText值设置为XML中最后编辑文本的值。通常会设置简单的(Basic Android EditText)编辑文本值。

在此处输入图片说明

代号

我从发生此问题的项目中复制了代码。

主要活动

class MainActivity : AppCompatActivity() {

     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)

         first_custom_edit_text.header = "First header"
         first_custom_edit_text.setText("First text")

         third_custom_edit_text.header = "Third header"
         third_custom_edit_text.setText("Third text")

         first_simple_edit_text.setText("First simple - Not affected")

         second_custom_edit_text.header = "Second header"
         second_custom_edit_text.setText("Second text")

         second_simple_edit_text.setText("Second simple - Not affected")
     }
}

CustomEditText

class CustomEditText : LinearLayout {
    fun setText(value: String?){
        this.input_edit_text.text = Editable.Factory.getInstance().newEditable(value ?: "")
    }

    fun getText(): String {
        return this.input_edit_text.text.toString()
    }

    var header: String?
        get() = this.header_text_view.text.toString()
        set(value) {
            this.header_text_view.text = Editable.Factory.getInstance().newEditable(value ?: "")
        }

    constructor(context: Context) : super(context){
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs){
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        inflate(context, R.layout.ui_custom_edit_text, this)
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <com.example.customedittextbug.CustomEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/first_custom_edit_text"/>

    <com.example.customedittextbug.CustomEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/second_custom_edit_text"/>

    <EditText
        tools:hint="[email protected]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="-4dp"
        android:layout_marginRight="-4dp"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:inputType="text"
        android:id="@+id/first_simple_edit_text"/>

    <com.example.customedittextbug.CustomEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/third_custom_edit_text"/>


    <EditText
        tools:hint="[email protected]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="-4dp"
        android:layout_marginRight="-4dp"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:inputType="text"
        android:id="@+id/second_simple_edit_text"/>

</LinearLayout>

ui_custom_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
            tools:text="Input header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:textSize="17sp"
            android:id="@+id/header_text_view"/>
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/validations_errors_holder"/>
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/common_input_holder">
        <EditText
                tools:hint="[email protected]"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="-4dp"
                android:layout_marginRight="-4dp"
                android:textColor="@android:color/black"
                android:textSize="18sp"
                android:inputType="text"
                android:id="@+id/input_edit_text"/>
        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@+id/input_edit_text"
                android:layout_centerVertical="true"
                android:layout_marginEnd="4dp"
                android:layout_marginStart="4dp"
                android:gravity="end"
                android:orientation="horizontal"
                android:id="@+id/right_view_holder"/>
    </RelativeLayout>
</LinearLayout>

更新

我的问题得到解答后,我发现了这两本指南,都很好地解释了如何解决此问题。

链接1链路2

瑞安·M

状态恢复由ID设置键,并且您的所有自定义视图都有一个具有相同ID的子视图:input_edit_text因此,它们都恢复到相同的状态,因为它们都得到了保存在该ID下的最后一个。

你可以通过设置避免这种android:saveEnabled="false"EditText(尽管你可能会想要做你的实例状态自己保存/恢复CustomEditText)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

自定义编辑文本

Unity-如何创建自定义编辑器以动态更改另一个具有自定义大小的列表中列表的大小?

自定义带边框的编辑文本

在Android中设计自定义编辑文本

如何从另一个指令更改指令自定义属性的值

自定义“缩放”编辑文本无法编辑

如何在文本中获取自定义标签,并放入另一个文本?

在另一个自定义UIView中设置/更改嵌套自定义UIView的变量

如何从另一个“高级自定义字段”获取“高级自定义字段”选择值?

自定义HTML元素扩展了另一个自定义元素

从另一个自定义函数调用Google表格自定义函数

在另一个自定义 UIView 中嵌入自定义 UITableViewCell?

从另一个自定义重定向到自定义 html 页面

使用另一个向量定义的自定义间隔对向量中的Bin值

如何通过自定义属性在另一个元素内的元素内获取文本

可以自定义 ListTile 以容纳标题、子标题和另一个文本行吗?

带有子项的自定义视图(按钮,编辑文本等)

在片段中编辑文本的自定义列表视图

Android自定义软键盘-如何清除编辑文本(提交的文本,...)

ChartJS:获取单个值减去自定义图例中的另一个值

从另一个组件更改自定义组件的 css 类

Salesforce Lighting - 内联编辑 - 在自定义下拉列表中更改值时显示文本框

在Angularjs中添加另一个自定义插值器

根据另一个表中的自定义属性过滤值

基于另一个属性值的属性上的自定义JsonConverter

基于另一个单元格值的自定义货币格式

将一个 Swing 自定义表单插入另一个自定义表单

从WooCommerce中的自定义文本字段中保存一个空值

在自定义对话框中编辑文本始终返回空值