Setting attributes of an EditText added dynamicall

2019-07-21 11:55发布

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条回答
乱世女痞
2楼-- · 2019-07-21 12:21

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.

查看更多
登录 后发表回答