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

时间:2016-08-05 10:45:39

标签: java android android-layout listview android-studio

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

3 个答案:

答案 0 :(得分:0)

cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

    }
});

答案 1 :(得分:0)

更好的处理方式,以这种方式轻松进行自定义。

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>

答案 2 :(得分:0)

我在下面尝试过,根据你的情况使用它。

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