我在放射性按钮的放射性组下面有定义。如何正确实现onClickListener?我得到了ArrayIndexOutOfBoundsException。我不知道出了什么问题。
public void createRadioButton(Integer amount, LinearLayout layout, Integer text) {
AppCompatTextView txt = new AppCompatTextView(activity);
AppCompatButton btn = new AppCompatButton(activity);
btn.setText("Click");
txt.setText(text);
final AppCompatRadioButton[] rb = new AppCompatRadioButton[amount];
final RadioGroup rg = new RadioGroup(activity);
rg.setOrientation(RadioGroup.VERTICAL);
for( i=0; i<amount; i++){
rb[i] = new AppCompatRadioButton(activity);
rg.addView(rb[i]);
rb[i].setId(i + 100);
rb[i].setText("Test " + i);
}
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = rg.getCheckedRadioButtonId();
rb[i] = (AppCompatRadioButton) activity.findViewById(selectedId);
Toast.makeText(activity, "VALUE" rb[i].getText(), Toast.LENGTH_SHORT).show();
}
});
layout.addView(txt);
layout.addView(rg);
layout.addView(btn);
}
答案 0 :(得分:3)
for( i=0; i<amount; i++){
rb[i] = new AppCompatRadioButton(activity);
rg.addView(rb[i]);
rb[i].setId(i + 100);
rb[i].setText("Test " + i);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = rg.getCheckedRadioButtonId();
rb[i] = (AppCompatRadioButton) activity.findViewById(selectedId);
Toast.makeText(activity, "VALUE" rb[i].getText(), Toast.LENGTH_SHORT).show();
}
});
}
如果你想为循环的每个项添加一个onclicklistener,你需要扩展你的for循环,这样它也会迭代你的onclicklistener,就像这样。
答案 1 :(得分:2)
使用setOnCheckedChangeListener进行radiogroup
像这样使用
radiogroup_gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radiobutton_male :
break;
case R.id.radiobutton_female :
break;
default:
break;
}
}
});
答案 2 :(得分:1)
在循环结束后,您的i
值将等于amount
,并且您在i
使用rb[i] = (AppCompatRadioButton) activity.findViewById(selectedId);
amount-1
并且您的列表大小等于ArrayIndexOutOfBoundsException
这就是您获得<ion-content>
<div class="list">
<label class="item item-input item-floating-label">
<span class="input-label">Email Address</span>
<input type="email" placeholder="Email Address">
</label>
<div class="padding-horizontal">
<button class="button button-positive button-block" ng-click="clicked()">
Create Account
</button>
</div>
</div>
</ion-content>
答案 3 :(得分:1)
您的代码中有一些错误。首先,您的参数文本应为&#34; String&#34;,而不是整数。但是,代码中的主要问题是您用代码覆盖了最后一个radioButton(rb [i])。你不应该这样做。 最好的方法是在循环中声明变量i,并在用户点击它时创建一个新的RadioButton。
Ps:您验证的解决方案效率低下,因为每次创建按钮时都会创建一个点击侦听器。或者它总是更新同一个listerner,因为你只有一个按钮。
尝试以下代码;)
public void createRadioButton(Integer amount, LinearLayout layout, String text) {
AppCompatTextView txt = new AppCompatTextView(activity);
AppCompatButton btn = new AppCompatButton(activity);
btn.setText("Click");
txt.setText(text);
final AppCompatRadioButton[] rb = new AppCompatRadioButton[amount];
final RadioGroup rg = new RadioGroup(activity);
rg.setOrientation(RadioGroup.VERTICAL);
for(int i=0; i<amount; i++){
rb[i] = new AppCompatRadioButton(activity);
rg.addView(rb[i]);
rb[i].setId(i + 100);
rb[i].setText("Test " + i);
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = rg.getCheckedRadioButtonId();
AppCompatRadioButton button= (AppCompatRadioButton) activity.findViewById(selectedId);
Toast.makeText(activity, "VALUE" + button.getText(), Toast.LENGTH_SHORT).show();
}
});
layout.addView(txt);
layout.addView(rg);
layout.addView(btn);
}