Why EditText in a custom compound view is re-using the text entered in another compound view instance?

前端 未结 6 668
一个人的身影
一个人的身影 2020-12-24 08:21

I\'m trying to write a custom compound view composed by a TextView and an EditText, _compound_view.xml_:



        
6条回答
  •  情歌与酒
    2020-12-24 08:31

    You need to disable SaveEnabled property of EditText using android:saveEnabled="false"

    In your custom view, you are inflating layout from XML which has ID defined. Android OS has default functionality to save and restore the state of the view if the view has ID defined.

    It means that it will save the text of the EditText when Activity gets paused and restore automatically when Activity gets restored. In your case, you are using this custom view multiple times and that is inflating the same layout so your all EditText have the same ID. Now when Activity will get pause Android will retrieve the value of the EditText and will save against their ID but as you have the same ID for each EditText, values will get override and so it will restore same value in all your EditText.

提交回复
热议问题