简体   繁体   English

自定义复选框未选中setonclicklistener方法不起作用

[英]Custom checkbox is not check on setonclicklistener method is not working

setonclicklistener is not working on it how i select this checkbox setonclicklistener不起作用,如何选择此复选框

public void startQuizClick(View v) {
        count++;
        HashMap<String, String> h1 = questionlist.get(k);
       question.setText(h1.get("question_text"));
        val = Integer.parseInt(h1.get("total_options"));
    for (int i = 0; i < Integer.parseInt(h1.get("total_options")); i++)
    {
            HashMap<String, String> h2 = optionlist.get(i);
           // cb is custom checkbox
            cb = new CheckBox(this);
            cb.setId(i);
            cb.setEnabled(true);
            cb.setTextColor(getResources().getColor(R.color.textColor));
            cb.setText(h2.get("option_text" + i));
            cb.setTextSize(14);
           //linear layout
           linearLay1.addView(cb);
        }
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //do stuff

    }
});

Better way of handling and much easy for customization in this way. 更好的处理方式,以这种方式易于定制。

public void startQuizClick(View v) {
        count++;
        HashMap<String, String> h1 = questionlist.get(k);
        question.setText(h1.get("question_text"));
        val = Integer.parseInt(h1.get("total_options"));

        LayoutInflater inflater = (LayoutInflater) ACTIVITYNAME.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int i = 0; i < Integer.parseInt(h1.get("total_options")); i++) {
            View view = inflater.inflate(R.layout.row_layout, null);
            CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox);

            HashMap<String, String> h2 = optionlist.get(i);
            cb.setId(i);
            cb.setEnabled(true);
            cb.setTextColor(getResources().getColor(R.color.textColor));
            cb.setText(h2.get("option_text" + i));
            cb.setTextSize(14);
            cb.setOnCheckedChangeListener(this);
            cb.setTag(i);
            linearLay1.addView(view);
        }
    }

onCheckedChanged() onCheckedChanged()

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch ((int)buttonView.getTag()) {
                case 0:

                    break;
                case 1:

                    break;
                case 2:

                    break;
                case 3:

                    break;
                case 4:

                    break;

                //...
            }
        }

row_layout.xml row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New CheckBox" />
</LinearLayout>

I tried below, Use this according to your scenario. 我在下面尝试过,根据您的情况使用它。

  public void startQuizClick() {

        HashMap<String, String> h1 = new HashMap<>();

        for (int i = 0; i < 5; i++) {

            // cb is custom checkbox
            CheckBox cb = new CheckBox(this);
            cb.setLayoutParams(new LinearLayout.LayoutParams(100,100));
            cb.setId(i);
            cb.setEnabled(true);
            cb.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.colorAccent));
            cb.setText("test");
            cb.setTextSize(14);

            cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Log.e("item checked","ischedked"+isChecked);
                }
            });
            //linear layout
            lli.addView(cb);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM