如何将 setOnCheckedChangeListener 用于多个复选框 [动态视图]

How to use setOnCheckedChangeListener for multiple checkbox [Dynamic View]

我将创建一个动态视图。在此视图中,我可以使用 5 CheckBox

设计一个动态视图

我将尝试使用以下动态视图代码:

private void chkdynamic() {
        final String name[] = {"Credit Card", "Debit Card","Bank Deposit","in-app Purchase", "PayPal"};
        /*int Array_Count=0;
        String[] Str_Array = new String[0];

        Array_Count = Str_Array.length;*/

        for (int i = 0; i < name.length; i++)
        {

            final View v = new View(this);
            v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 5));
            v.setBackgroundColor(getResources().getColor(R.color.border_gray));

            LinearLayout row = new LinearLayout(this);
            row.setOrientation(LinearLayout.VERTICAL);
            row.setId(i);
            row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            final CheckBox checkBox = new CheckBox(activity);
            checkBox.setId(i);
            checkBox.setText(name[i]);
            checkBox.setTextColor(getResources().getColor(R.color.gray_text));
            checkBox.setButtonDrawable(getResources().getDrawable(R.drawable.chk_btn));
            checkBox.setPadding(20,60,0,60);
            checkBox.setTextSize(20);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(60, 0, 0, 0);
            checkBox.setLayoutParams(params);
            row.addView(checkBox);
            row.addView(v);
            ll_dynamic.addView(row);
        }
    }

根据上面的代码 5 根据名称长度创建复选框。

我现在的问题是 如何 setOnCheckedChangeListener 复选框

如果我选中 CheckBox 一个然后另一个 CheckBox 取消选中如何在动态视图中实现这一点。

请给我建议任何解决方案。

public class MainActivity extends AppCompatActivity {

LinearLayout ll_dynamic;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ll_dynamic=(LinearLayout)findViewById(R.id.hhh);
    final String name[] = {"Credit Card", "Debit Card","Bank Deposit","in-app Purchase", "PayPal"};
    /*int Array_Count=0;
    String[] Str_Array = new String[0];

    Array_Count = Str_Array.length;*/

    for (int i = 0; i < name.length; i++)
    {

        final View v = new View(this);
        v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 5));

        LinearLayout row = new LinearLayout(this);
        row.setOrientation(LinearLayout.VERTICAL);
        row.setId(i);
        row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
        final CheckBox checkBox = new CheckBox(this);
        checkBox.setId(1000+i);
        checkBox.setText(name[i]);
        checkBox.setPadding(20,60,0,60);
        checkBox.setTextSize(20);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(60, 0, 0, 0);
        checkBox.setLayoutParams(params);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    for (int j = 0; j < name.length; j++) {
                        if (buttonView.getId() != 1000+j) {
                            CheckBox chk = (CheckBox)ll_dynamic.findViewById(1000+j);
                            chk.setChecked(false);
                        }
                    }
                }
            }
        });
        row.addView(checkBox);

        row.addView(v);
        ll_dynamic.addView(row);
    }
}

}