本文介绍了检查动态添加的复选框的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以如下方式动态添加我的复选框:
I add my checkboxes dynamically like this:
public void addFiles()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.filesList);
if(!FileManagerActivity.finalAttachFiles.isEmpty())
{
for (final File file:FileManagerActivity.finalAttachFiles)
{
Log.i("what I've got", file.toString());
View line = new View(this);
line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
line.setBackgroundColor(0xAA345556);
informationView= new CheckBox(this);
informationView.setTextColor(Color.BLACK);
informationView.setTextSize(16);
informationView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
informationView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.file_icon, 0, 0, 0);
informationView.setText(file.getName().toString());
layout.addView(informationView, 0);
layout.addView(line, 1);
layout.postInvalidate();
}
}
}
我有一个按钮,该onClick事件应获取那些复选框的可检查状态,并删除已选中的复选框...我该如何实现呢?
And I have a button, which onClick event should get the checkable state of those checkboxes and delete which were checked...How can I implement this?
推荐答案
您可以添加以下复选框:
your can add your checkboxes like:
for(int i=0;i<totalCB;i++){
CheckBox chB=new CheckBox(context);
...
chB.setId(i);
layout.add(...);// add checkbox to view
}
现在,点击按钮
for(int i=0;i<totalCB;i++){
CheckBox cb=(CheckBox)findViewById(i);
boolean checked=cd.isChecked();// status of checkbox
if(checked){
// perform action
}
}
这篇关于检查动态添加的复选框的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!