我已动态创建布局并添加了复选框。代码如下,
//Load dynamic view
List<Integer> checkBoxIdList = new ArrayList<Integer>();
int id = 0;
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("CatchInfo");
tv.setTextColor(Color.rgb(200,0,0));
ll.addView(tv);
for(int i=0;i<list2.size();i++)
{
ck = new CheckBox(this);
ck.setId(id);
checkBoxIdList.add(id);
ck.setText(list2.get(i));
ll.addView(ck);
}
Button btnHome = new Button(this);
btnHome.setText("Home");
ll.addView(btnHome);
Button btnSubmit = new Button(this);
btnSubmit.setText("Submit");
ll.addView(btnSubmit);
this.setContentView(sv);
我正在尝试使用click listner获取复选框的值,如下所示:
ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(!isChecked){
Toast.makeText(getApplicationContext(), "Yes",
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "No",
Toast.LENGTH_LONG).show();
}
}
});
但点击操作仅在最后一个复选框中发生,而在其他复选框中则没有响应。如何解决这个问题以及如何获得价值?
答案 0 :(得分:1)
你必须这样做。
for(int i=0;i<list2.size();i++)
{
CheckBox ck = new CheckBox(this);
ck.setId(id);
checkBoxIdList.add(id);
ck.setText(list2.get(i));
ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(!isChecked){
Toast.makeText(getApplicationContext(), "Yes",
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "No",
Toast.LENGTH_LONG).show();
}
}
});
ll.addView(ck);
}
答案 1 :(得分:1)
你忘了增加id变量。 那就是你要添加的所有复选框都会得到相同的ID。
List<CheckBox> checkBoxesList2 = new ArrayList<CheckBox>();
for(int i=0;i<list2.size();i++)
{
ck = new CheckBox(this);
ck.setId(id);
checkBoxIdList.add(id);
checkBoxesList2.add(ck);
id++;
ck.setText(list2.get(i));
ll.addView(ck);
}
for(int i=0;i<checkBoxesList2.size();i++){
ck =(CheckBox) checkBoxesList2.get(i);
ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(!isChecked){
Toast.makeText(getApplicationContext(), "Yes",
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "No",
Toast.LENGTH_LONG).show();
}
}
});
}
答案 2 :(得分:1)
您需要创建函数来处理onCheckedChangeListener。请参阅下面的代码。
List<Integer> checkBoxIdList = new ArrayList<Integer>();
int id = 0;
String[] list2 = {"A", "B", "C", "D", "E"};
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("CatchInfo");
tv.setTextColor(Color.rgb(200,0,0));
ll.addView(tv);
for(int i=0;i<list2.size();i++)
{
ck = new CheckBox(this);
ck.setId(id);
checkBoxIdList.add(id);
ck.setTag(list2[i]); // set the tag values so that you can refer to them later.
ll.addView(ck);
ck.setOnCheckedChangeListener(handleCheck(ck)); //Calling the function, add this line in your code
}
Button btnHome = new Button(this);
btnHome.setText("Home");
ll.addView(btnHome);
Button btnSubmit = new Button(this);
btnSubmit.setText("Submit");
ll.addView(btnSubmit);
this.setContentView(sv);
以下是处理检查事件的功能
private OnCheckedChangeListener handleCheck (final CheckBox chk)
{
return new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(!isChecked){
Toast.makeText(getApplicationContext(), "You unchecked " + chk.getTag(),
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "You checked " + chk.getTag(),
Toast.LENGTH_LONG).show();
}
}
};
}