Setting attributes of an EditText added dynamicall

2019-07-21 12:27发布

问题:

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.