简体   繁体   中英

How to change name of Radio button dynamically in android studio?

I am dynamically creating radio buttons as per the count given in the edit text. I want to edit the name of the radiobutton after it gets created dynamically in the app, like the user has the privilege to name the radio button in the app. How should I do that android studio This is my code for creating dyanmic radiobuttons so far. Here in the code rbn.setText("Radiobutton"+i) names the radiobutton as Radiobutton1, Radiobutton2, etc. instead of that I want to change the name of that in the app dynamically and write the name of the radiobutton one by one, help would appreciated please!

package com.example.EZPoll;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class Public_create extends AppCompatActivity {
    EditText mEtNumOfRadioBtns;
    Button mBtnAdd;
    RadioGroup mRgAllButtons;
    String option_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public_create);
        mEtNumOfRadioBtns = findViewById(R.id.et_no);
        mBtnAdd = (Button) findViewById(R.id.btn);
        mRgAllButtons = (RadioGroup)findViewById(R.id.radiogroup);
        mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number=Integer.parseInt(mEtNumOfRadioBtns.getText().toString().trim());
                for (int i = 1; i <= number ; i++) {
                    RadioButton rbn = new RadioButton(Public_create.this);
                    rbn.setId(View.generateViewId());
                    rbn.setText("Radiobutton "+i);
                    mRgAllButtons.addView(rbn);
                }
            }
        });
    }
}```

The problem that you have now is that you can't reference the buttons created in the loop through user interaction. I suggest creating a List<myData> which contains a RadioButton and an EditText , show them in a RecyclerView or ListView .

Then the user can edit the name of the RadioButton throught the EditText one by one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM