Android的 - 构建code动态表单 [英] Android - build dynamic form from code
问题描述
我在我的活动,以构建一个动态的形式取决于是从XML popullated通过HTTP检索到的数据。
I have to build a dynamic form in my activity depending on the data retrieved via HTTP that is popullated from XML.
这可能是一个或多个 RadioGroup中
s分别有整整三单选
秒。之后RadioGroups我需要把两个的EditText
和一个复选框
控制与事后提交按钮。
This could be one or more RadioGroup
s each with exactly three RadioButton
s. After RadioGroups I need to place two EditText
and one CheckBox
control with submit button afterwards.
我有prepared一个的LinearLayout
垂直方向和唯一的ID从code解决,我希望我可以创建中的表单控件没有定义在Android的XML布局,并增加该Java code 的LinearLayout
。
I have prepared a LinearLayout
with vertical orientation and unique ID to be addressed from code and I expect that I can create a form control within a Java code without defining in android XML layout and adding to this LinearLayout
.
我在Google上搜寻了几个小时也没有找到任何例子,如何做到这一点。
I was googling for a few hours but could not find any example how to do this.
任何人都可以请提供一些例子,如何创建如一个 RadioGruop
1-2 单选
和它添加到的LinearLayout
(即prepared在XML布局)?
Could anyone please provide some example how to create e.g. one RadioGruop
with 1-2 RadioButton
s and add it to the LinearLayout
(that is prepared in XML layout)?
非常感谢任何意见!
推荐答案
这些小工具可以创建一个像所有其他部件:
These widgets can be create like every other widgets:
final Context context; /* get Context from somewhere */
final LinearLayout layout = (LinearLayout)findViewById(R.id.your_layout);
final RadioGroup group = new RadioGroup(context);
final RadioButton button1 = new RadioButton(context);
button1.setId(button1_id); // this id can be generated as you like.
group.addView(button1,
new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT));
final RadioButton button2 = new RadioButton(context);
button1.setId(button2_id); // this id can be generated as you like.
button2.setChecked(true);
group.addView(button2,
new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT));
layout.addView(group,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
我没有测试此code,所以它可能包含一些错误。但我希望你明白了吧。
I haven't tested this code, so it may contain some errors. But I hope you'll get the idea.
这篇关于Android的 - 构建code动态表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!