如何使用 spinner int 值以编程方式添加视图?
How to use spinner int value to programatically add views?
相关代码如下:
mspin=(Spinner) findViewById(R.id.spinner);
Integer[] items = new Integer[]{1,2,3,4,5,6,7,8,9,10};
ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, items);
mspin.setAdapter(adapter);
RG = (RadioGroup) findViewById(R.id.radioGroup);
mspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int TYPE = Integer.parseInt(mspin.getSelectedItem().toString());
setRadios(TYPE);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
public void setRadios(int mk) {
for(int i=0; i==mk; i++){
RadioButton bg = new RadioButton(this);
bg.setText("hello, this is mk");
RG.addView(bg);
}
}
我正在尝试从微调器获取一个 int 值,然后将那么多 RadioButton 添加到 RadioGruop RG。但是,当我 运行 代码在我单击微调器值时它什么也不做。
试试这个方法:
public void setRadios(int number) {
for (int row = 0; row < 1; row++) { //add to first row
RadioGroup rg = new RadioGroup(this);
rg.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());
rg.addView(rdbtn);
}
((ViewGroup) findViewById(R.id.radiogroup)).addView(rg);
}
}
在XML中:
<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" />
您没有将 radiobutton
添加到 viewgroup
int TYPE = Integer.parseInt(mspin.getSelectedItem().toString());
这是错误的尝试下面的代码。它会起作用。
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int TYPE=Integer.parseInt(adapter.getItem(position).toString());
setRadios(TYPE);
}
您的 for
循环有误,请将此 for(int i=0; i==mk; i++)
更改为此 for(int i=0; i<=mk; i++)
并且您还可以简化如何将 int Type
更改为 int Type = items[position]
相关代码如下:
mspin=(Spinner) findViewById(R.id.spinner);
Integer[] items = new Integer[]{1,2,3,4,5,6,7,8,9,10};
ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, items);
mspin.setAdapter(adapter);
RG = (RadioGroup) findViewById(R.id.radioGroup);
mspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int TYPE = Integer.parseInt(mspin.getSelectedItem().toString());
setRadios(TYPE);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
public void setRadios(int mk) {
for(int i=0; i==mk; i++){
RadioButton bg = new RadioButton(this);
bg.setText("hello, this is mk");
RG.addView(bg);
}
}
我正在尝试从微调器获取一个 int 值,然后将那么多 RadioButton 添加到 RadioGruop RG。但是,当我 运行 代码在我单击微调器值时它什么也不做。
试试这个方法:
public void setRadios(int number) {
for (int row = 0; row < 1; row++) { //add to first row
RadioGroup rg = new RadioGroup(this);
rg.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());
rg.addView(rdbtn);
}
((ViewGroup) findViewById(R.id.radiogroup)).addView(rg);
}
}
在XML中:
<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" />
您没有将 radiobutton
添加到 viewgroup
int TYPE = Integer.parseInt(mspin.getSelectedItem().toString());
这是错误的尝试下面的代码。它会起作用。
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int TYPE=Integer.parseInt(adapter.getItem(position).toString());
setRadios(TYPE);
}
您的 for
循环有误,请将此 for(int i=0; i==mk; i++)
更改为此 for(int i=0; i<=mk; i++)
并且您还可以简化如何将 int Type
更改为 int Type = items[position]