Difference between android:id and android:labelFor?

后端 未结 5 619
春和景丽
春和景丽 2020-12-25 10:37

I wrote a simple layout which has an EditText, but it\'s showing the following warning message:

“No label views point to this text field”

5条回答
  •  失恋的感觉
    2020-12-25 11:09

    in addition to all answers if u don't use xml files for apps this is a brief explanation what for serves VIEW ID :

    (btw in my opinion using xml sucks - my only xml file is manifest :D generated by gradle)

    @IdRes - annotation for resource id

    /** define resource id for view */
    @IdRes 
    int TEXT_VIEW_ID  = "111111";
    
    /** create edit tex in code */
    EditText myTextView = new EditText(Context);
    /** set view id */
    myTextView.setID(TEXT_VIEW_ID);
    /** set layout params etc then attach or inflate as u wish to view hierarchy */    
    
    /** use view id to find view */
    EditText etFound = (EditText) View.findViewById(TEXT_VIEW_ID);
    

    ps. ID is mandatory to preserve state of hierarchy view when Activity.onSaveInstanceState(Bundle) is used - so if u create in code (VIEW / WIDGET / LAYOUT etc.) don't forget to set it.

提交回复
热议问题