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

标签 android button dynamic count radio-button

我已经尝试过这段代码。当模拟器启动时,它将在一行中显示三个单选按钮。但我需要一个按钮事件。 IE;如果我单击按钮,它应该询问单选按钮的数量。那么如果我给出计数,它必须根据给定的计数显示单选按钮。例如,如果我将计数设为 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>

最佳答案

请在代码下方找到,我在 xml 布局中创建了一个“EditText”和一个“Button”。在'EditText'中输入一个数字,然后单击按钮,相同的数字。的单选按钮将添加到布局中。

这是你的ActivityMain

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>

关于android - 如何根据给定的计数动态添加单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19380526/

相关文章:

android 乘法按钮大小写开关

button - iOS自定义按钮类似于barbutton

javascript - 在 Three.js 中动态地将一个网格替换为另一个新网格

android - Firestore Android 保存失败 - 无法访问 Firestore 后端

java - 找不到符号方法 setAnimationListener

Android:在 FCM 中延迟接收消息(onMessageReceived)

Android 在 webview 中屏蔽广告

android - 按钮在我的 Activity 中不起作用

Mysql - 将变量传递给子查询 From 子句

java - 动态改变嵌套for循环的数量