Android 如何添加或删除两个动态复选框值

Android 如何添加或删除两个动态复选框值,android,Android,我有动态复选框,它们的值如下 复选框1=$20 复选框2=$10 如果单击一个复选框,则该值应反映在文本视图中;如果选择两个复选框,则该值应添加到该文本视图中,或取消选中该复选框,则该值应从该文本视图中减去。 此复选框值是动态出现的,因此我需要添加/减去这两个值,并在文本视图中显示总值。 我该怎么做?请帮帮我。 提前感谢您选中复选框,我们有一个事件侦听器作为OnCheckedChangeListener 为两个复选框(例如chkBox1、chkBox2)覆盖此侦听器,以及 检查 int valu

我有动态复选框,它们的值如下 复选框1=$20 复选框2=$10 如果单击一个复选框,则该值应反映在文本视图中;如果选择两个复选框,则该值应添加到该文本视图中,或取消选中该复选框,则该值应从该文本视图中减去。 此复选框值是动态出现的,因此我需要添加/减去这两个值,并在文本视图中显示总值。 我该怎么做?请帮帮我。
提前感谢您选中复选框,我们有一个事件侦听器作为OnCheckedChangeListener

为两个复选框(例如chkBox1、chkBox2)覆盖此侦听器,以及

检查

int value1,value2;
if(chkBox1.isChecked){
//store value of this checkbox into one variable  i.e.
value1 = value of chkBox1;
}else{
// reset value for this chkBox1 i.e
value1 = 0;
}

if(chkBox2.isChecked){
//store value of this checkbox into one variable  i.e.
value2 = value of chkBox2;
}else{
// reset value for this chkBox2 i.e
value2 = 0;
}
int sum = value1 + value2;
textView.setText(sum + "");

使用OnCheckedChangeListeners执行任务

 chkBox1.setOnCheckedChangeListener(eventListener);
 chkBox2.setOnCheckedChangeListener(eventListener);

 CompoundButton.OnCheckedChangeListener eventListener=new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId())
        {
            case R.id.chk1;
                if(isChecked)
                {
                    //do your first task
                    //set your textbox values
                }
                break;
            case R.id.chk2:
                if(isChecked)
                {
                    //do your second task
                    //set your second text values
                }
                break;
        }


    }
};
如果您没有太多复选框,并且迭代很好,那么您可以尝试下面的代码。
public void addUserType(ProObj obj)
{
最终复选框cb=新复选框(getActivity());
cb.setText(obj.getamount());
cb.setTag(obj);
layUsertype.post(新的Runnable(){
公开募捐{
layUsertype.addView(cb);
}
});
//任何侦听器方法
cb.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
双和=0.0;
int childcount=layUsertype.getChildCount();
for(int i=0;i
Hear是经过测试的代码,可以正常工作

试试这个

public class MainActivity extends Activity implements OnCheckedChangeListener {

CheckBox cb1, cb2;
TextView tv1;
int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cb1 = (CheckBox) findViewById(R.id.checkBox1);
    cb2 = (CheckBox) findViewById(R.id.checkBox2);
    tv1 = (TextView) findViewById(R.id.textView1);

    cb1.setOnCheckedChangeListener(this);
    cb2.setOnCheckedChangeListener(this);
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (buttonView.getId() == R.id.checkBox1) {
        if (isChecked) {
            count = count + Integer.parseInt(cb1.getText().toString());
        } else {
            if (count != 0) {
                count = count - Integer.parseInt(cb1.getText().toString());
            }
        }
        tv1.setText("" + count);
    }
    if (buttonView.getId() == R.id.checkBox2) {
        if (isChecked) {
            count = count + Integer.parseInt(cb2.getText().toString());
        } else {
            if (count != 0) {
                count = count - Integer.parseInt(cb2.getText().toString());
            }
        }
        tv1.setText("" + count);
    }
}
}

祝您好运。

您好,vrund,但是我的复选框是动态出现的,这意味着从web服务扫描您可以向我显示您从服务中获得的数据类型吗?
public class MainActivity extends Activity implements OnCheckedChangeListener {

CheckBox cb1, cb2;
TextView tv1;
int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cb1 = (CheckBox) findViewById(R.id.checkBox1);
    cb2 = (CheckBox) findViewById(R.id.checkBox2);
    tv1 = (TextView) findViewById(R.id.textView1);

    cb1.setOnCheckedChangeListener(this);
    cb2.setOnCheckedChangeListener(this);
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (buttonView.getId() == R.id.checkBox1) {
        if (isChecked) {
            count = count + Integer.parseInt(cb1.getText().toString());
        } else {
            if (count != 0) {
                count = count - Integer.parseInt(cb1.getText().toString());
            }
        }
        tv1.setText("" + count);
    }
    if (buttonView.getId() == R.id.checkBox2) {
        if (isChecked) {
            count = count + Integer.parseInt(cb2.getText().toString());
        } else {
            if (count != 0) {
                count = count - Integer.parseInt(cb2.getText().toString());
            }
        }
        tv1.setText("" + count);
    }
}
}