How to add radio button dynamically as per the given number of counts?

前端 未结 4 797
心在旅途
心在旅途 2020-12-01 03:28

I have tried this code..It will display three radio buttons in a single row when the emulator starts. But I need a button event for this. i.e; if I click the button, it shou

4条回答
  •  [愿得一人]
    2020-12-01 04:18

    Add a Button and EditText in xml and take input from editText to variable inputValue and try this,

    public class MyActivity extends Activity {
    
        /**
         * Called when the activity is first created.
         */
        LinearLayout ll=null;
        ViewGroup   vwgroup;
        Button btnClick;    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            vwgroup=((ViewGroup)findViewById(R.id.radiogroup)
            btnClick=(Button)findViewById(R.id.button1);
    
    
            btnClick.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    if(ll!=null)
                        viewgroup.removeView(ll);
                    ll = new LinearLayout(this);
                    for(int i = 1; i < inputValue; i++) {
                        RadioButton rdbtn = new RadioButton(this);
                        rdbtn.setId(View.generateViewId());
                        rdbtn.setText("Radio " + rdbtn.getId());
                        ll.addView(rdbtn);
                    }
                    vwgroup.addView(ll);
    
                }
            });
        }
    }
    

提交回复
热议问题