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



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

更好的处理方式,以这种方式进行自定义非常容易。

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((

        @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

<?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>

我在下面尝试过,根据您的场景使用它。

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

相关内容

  • 没有找到相关文章

最新更新