Setting attributes of an EditText added dynamically in Android

懵懂的女人 提交于 2020-01-05 02:23:07

问题


I am developping an android app which downloads an xml and displays a layout with a number of edittexts, checkboxes, spinners, etc. added dynamically like this:

LinearLayout ll = new LinearLayout(this);
EditText nameField = new EditText(this);
ll.addView(nameField);
ScrollView sv = new ScrollView(this);
sv.addView(ll);
setContentView(sv);

I'm having trouble with setting some properties to an EditText added this way. For examle android:maxLength attribute can easily be set in an xml layout but I found no method to do the same in the java code.

How can I do it when hawing to add dynamically?

Thanks, Zoltán from Hungary


回答1:


If you look at the XML attributes in the docs, it lists the corresponding method you can call in your java code for each attribute

So for example setting the maxLength attribute can be accomplished through the setFilters(InputFilter) method.



来源:https://stackoverflow.com/questions/4205775/setting-attributes-of-an-edittext-added-dynamically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!