Android - build dynamic form from code

后端 未结 1 1297
挽巷
挽巷 2021-01-03 09:42

I have to build a dynamic form in my activity depending on the data retrieved via HTTP that is popullated from XML.

This could be one or more RadioGroup

1条回答
  •  被撕碎了的回忆
    2021-01-03 10:41

    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));
    

    I haven't tested this code, so it may contain some errors. But I hope you'll get the idea.

    0 讨论(0)
提交回复
热议问题