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

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

    I got the same annoying problem and solved it with another solution than the ones suggested. When the custom view is inflated, I don't find them with IDs or tags, I get them using the getChildAt method. I did not have to save the instance state.

    Here is an example:

    Custom XML to inflate (custom_view.xml):

    
    
        
        
    
    

    You then just get the views this way during the view initialization:

    private void initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.custom_view, this, true);
    
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER);
    
        TextView mTitleTV = (TextView) getChildAt(0);
        EditText mContentET = (EditText) getChildAt(1);
    }
    

    IMPORTANT: you must remove all IDs from the XML

提交回复
热议问题