java 如何使用setOnCheckedChangeListener进行多个复选框[动态视图]

sycxhyv7  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(94)

我将创建一个动态视图。在这个视图中,我可以用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复选框,根据名称长度创建。
我的问题是现在如何i setOnCheckedChangeListener为复选框
如果我选中CheckBox一个,然后其他CheckBox取消选中如何在动态视图中实现这一点。
请给我一个解决办法。

63lcw9qa

63lcw9qa1#

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);
    }
}

}

相关问题