如何根据给定的计数动态添加单选按钮? [英] How to add radio button dynamically as per the given number of counts?

查看:162
本文介绍了如何根据给定的计数动态添加单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这个代码。当模拟器启动时,它会在一行显示三个单选按钮。但是我需要一个按钮事件。即:如果我点击按钮,它应该要求单选按钮的数量。那么如果我给出计数,它必须显示基于给定计数的单选按钮。例如,如果我将计数设置为3,它必须在一行中显示三个单选按钮。
您的帮助是非常感谢。
提前感谢。

  public class MyActivity extends Activity {
/ **
*在第一次创建活动时调用。
* /
@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.0encoding =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_centerHorizo​​ntal =true
android:layout_centerVertical =true/>

< / RelativeLayout>


解决方案

请在下面找到代码, EditText'和xml布局中的'Button'。在EditText中输入一个数字,然后单击按钮,同样没有。



<$> p $ p> public class ActivityMain extends Activity {

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



final EditText editText =(EditText)findViewById(R.id.et_no);

findViewById(R.id.btn).setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v){
// TODO自动生成方法存根


int number = Integer.parseInt(editText.getText()。toString());
addRadioButtons(number);
}
});

}

public void addRadioButtons(int number){

for(int row = 0; row< 1; row ++){
RadioGroup ll = new RadioGroup(this);
ll.setOrientation(LinearLayout.HORIZONTAL);

for(int i = 1; i <= number; 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);
}

}

}

,这里是您的布局文件




 < RadioGroup 
android:id =@ + id / radiogroup
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_centerHorizo​​ntal =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 =添加电台btn
android:id =@ + id / btn/>
< / LinearLayout>


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 should ask for number of radio buttons. then If I give the count, it must display the radio buttons based on the count given. For example, If I give the count as 3, it must display the three radio buttons in a single row. Your help is highly appreciated. Thanks in advance.

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

this is 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>

解决方案

Please find below the code, I have created an 'EditText' and a 'Button' in the xml layout. Input a number in the 'EditText' and click the Button , The same no. of radio buttons will be added in the Layout.

This is your ActivityMain

public class ActivityMain extends Activity {

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



        final EditText editText=(EditText)findViewById(R.id.et_no);

        findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                int number=Integer.parseInt(editText.getText().toString());
                addRadioButtons(number);
            }
        });

    }

    public void addRadioButtons(int number) {

    for (int row = 0; row < 1; row++) {
        RadioGroup ll = new RadioGroup(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);

        for (int i = 1; i <= number; 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);
    }

  }

}

and here is your layout file

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

这篇关于如何根据给定的计数动态添加单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆