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

前端 未结 6 661
一个人的身影
一个人的身影 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:33

    The issue is happening because of the id field on compound_view.xml
    From your code, I just noticed that you inflated compound_view layout file in CompoundView class.

    As soon as you create compound_view.xml and put android:id="@+id/textLabel" and android:id="@+id/textEdit" id in your layout xml file, android studio automatically create those ids into int values in R class for single time.

    So, when you put CompoundView twice time in your activity_main.xml layout file, you just creating two instance of CompoundView but, both instances textEdit and textLabel have only 1 address location for each one. So, they are pointing to same address locations which are declared in R class.


    That's why, whenever you change textEdit or textLabel text programatically, they also change other textEdit or textLabel which are presented in both of your CompoundView

提交回复
热议问题