android 如何按照给定的计数动态添加单选按钮?

2wnc66cl  于 2022-12-28  发布在  Android
关注(0)|答案(4)|浏览(105)

我试过这个代码。。当模拟器启动时,它会在一行中显示三个单选按钮。但是我需要一个按钮事件。如果我单击按钮,它应该询问单选按钮的数量。然后,如果我给予计数,它必须根据给定的计数显示单选按钮。例如,如果我给出计数为3,它必须在一行中显示三个单选按钮。

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        for(int row=0; row < 1; row++)
        {
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.HORIZONTAL);
            for(int i = 1; i < 4; i++) {
                RadioButton rdbtn = new RadioButton(this);
                rdbtn.setId((row * 2) + i);
                rdbtn.setText("Radio " + rdbtn.getId());
                ll.addView(rdbtn);
            }
            ((ViewGroup)findViewById(R.id.radiogroup)).addView(ll);
        }
    }
    }

这是xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity" >

    <RadioGroup
            android:id="@+id/radiogroup"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"/>

    </RelativeLayout>
6kkfgxo0

6kkfgxo01#

请看下面的代码,我已经在xml布局中创建了一个“编辑文本”和一个“按钮”。在“编辑文本”中输入一个数字,然后单击按钮,相同数量的单选按钮将添加到布局中。
这是您的活动主页

public class ActivityMain extends AppCompatActivity implements View.OnClickListener {

    EditText mEtNumOfRadioBtns;
    Button mBtnAdd;
    String TAG = "TestActivity";
    RadioGroup mRgAllButtons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //
        mEtNumOfRadioBtns = findViewById(R.id.et_no);
        mBtnAdd = findViewById(R.id.btn);
        mRgAllButtons = findViewById(R.id.radiogroup);
        //
        mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number = Integer.parseInt(mEtNumOfRadioBtns.getText().toString().trim());
                addRadioButtons(number);
            }
        });
    }

    public void addRadioButtons(int number) {
        mRgAllButtons.setOrientation(LinearLayout.HORIZONTAL);
        //
        for (int i = 1; i <= number; i++) {
            RadioButton rdbtn = new RadioButton(this);
            rdbtn.setId(View.generateViewId());
            rdbtn.setText("Radio " + rdbtn.getId());
            rdbtn.setOnClickListener(this);
            mRgAllButtons.addView(rdbtn);
        }
    }

    @Override
    public void onClick(View v) {
        Log.d(TAG, " Name " + ((RadioButton)v).getText() +" Id is "+v.getId());
    }
}

这是名为“activity_main”的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" />

    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <EditText android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:hint="Enter no."
            android:inputType="number"
            android:id="@+id/et_no"/>

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Add Radio btn"
            android:id="@+id/btn"/>

    </LinearLayout>
    
</RelativeLayout>
uinbv5nw

uinbv5nw2#

请尝试以下操作:

RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup);
RadioGroup.LayoutParams rprms;

for(int i=0;i<3;i++){
      RadioButton radioButton = new RadioButton(this);
      radioButton.setText("new"+i);
      radioButton.setId(View.generateViewId());
      rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      rgp.addView(radioButton, rprms);
}
qgelzfjb

qgelzfjb3#

这是一种方法:

RadioGroup rgp = (RadioGroup) findViewById(R.id.radio_group);        
    int buttons = 5;
    for (int i = 1; i <= buttons ; i++) {
        RadioButton rbn = new RadioButton(this);
        rbn.setId(View.generateViewId());
        rbn.setText("RadioButton" + i);
        rgp.addView(rbn);
    }

但是如果你需要水平地做这件事,只要用setOrientation()方法加上方向(默认值是垂直的)就可以了:

RadioGroup rgp = (RadioGroup) findViewById(R.id.radio_group);
    rgp.setOrientation(LinearLayout.HORIZONTAL);
    int buttons = 5;
    for (int i = 1; i <= buttons; i++) {
        RadioButton rbn = new RadioButton(this);
        rbn.setId(View.generateViewId());
        rbn.setText("RadioButton" + i);
        rbn.setLayoutParams(params);
        rgp.addView(rbn);
    }

下面是完整的代码:
首先,在我们的布局中定义RadioGroup:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

MainActivity中的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Defining 5 buttons.
        int buttons = 5;
        AppCompatRadioButton[] rb = new AppCompatRadioButton[buttons];

        RadioGroup rgp = (RadioGroup) findViewById(R.id.radio_group);
        rgp.setOrientation(LinearLayout.HORIZONTAL);

        for (int i = 1; i <= buttons; i++) {
            RadioButton rbn = new RadioButton(this);
            rbn.setId(View.generateViewId());
            rbn.setText("RadioButton" + i);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
            rbn.setLayoutParams(params);
            rgp.addView(rbn);
        }

    }
}
pbossiut

pbossiut4#

在xml中添加ButtonEditText,并将editText中的输入内容添加到变量inputValue中,然后尝试执行以下操作:

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

            }
        });
    }
}

相关问题