如何增加单选按钮动态地每计数的给定数量? [英] How to add radio button dynamically as per the given number of counts?
问题描述
我已经试过这code..It将显示三个单选按钮,在单行模拟器启动时。但我需要一个按钮事件这一点。即;如果我按一下按钮,就应该要求的单选按钮数。那么如果我给的数量,它必须显示基于给定数量的单选按钮。例如,如果我给计数为3,它必须显示三个单选按钮在一行。 你的帮助是非常AP preciated。 先谢谢了。
公共类MyActivity延伸活动{
/ **
*第一次创建活动时调用。
* /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
对于(INT行= 0;行< 1;行++)
{
的LinearLayout LL =新的LinearLayout(本);
ll.setOrientation(LinearLayout.HORIZONTAL);
的for(int i = 1; I&4;;我++){
单选rdbtn =新的单选按钮(这一点);
rdbtn.setId((行* 2)+ I);
rdbtn.setText(无线+ rdbtn.getId());
ll.addView(rdbtn);
}
((ViewGroup中)findViewById(R.id.radiogroup))addView(LL)。
}
}
}
这是XML
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>
< RadioGroup中
机器人:ID =@ + ID / RadioGroup中
机器人:方向=垂直
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真/>
< / RelativeLayout的>
请在下面找到了code,我已经创建了一个的EditText和XML布局的按钮。输入一个数字。的EditText,然后点击按钮,同样没有。单选按钮将在布局中添加。
这是您的 ActivityMain
公共类ActivityMain延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
最后的EditText EDITTEXT =(EditText上)findViewById(R.id.et_no);
findViewById(R.id.btn).setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
INT数=的Integer.parseInt(editText.getText()的toString());
addRadioButtons(数);
}
});
}
公共无效addRadioButtons(INT编号){
对于(INT行= 0;行< 1;行++){
的LinearLayout LL =新的LinearLayout(本);
ll.setOrientation(LinearLayout.HORIZONTAL);
的for(int i = 1; I< =一些;我++){
单选rdbtn =新的单选按钮(这一点);
rdbtn.setId((行* 2)+ I);
rdbtn.setText(无线+ rdbtn.getId());
ll.addView(rdbtn);
}
((ViewGroup中)findViewById(R.id.radiogroup))addView(LL)。
}
}
}
和这里是您的布局文件
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>
< RadioGroup中
机器人:ID =@ + ID / RadioGroup中
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:方向=垂直/>
< LinearLayout中的android:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent>
< EditText上安卓layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:提示=输入没有。
机器人:ID =@ + ID / et_no/>
<按钮机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=创建电台BTN。
机器人:ID =@ + ID / BTN/>
< / LinearLayout中>
< / RelativeLayout的>
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++) {
LinearLayout ll = new LinearLayout(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
<?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_height="wrap_content"
android:layout_width="match_parent">
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Enter no."
android:id="@+id/et_no"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Create Radio btn"
android:id="@+id/btn"/>
</LinearLayout>
</RelativeLayout>
这篇关于如何增加单选按钮动态地每计数的给定数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!