【问题标题】:Custom checkbox is not check on setonclicklistener method is not working自定义复选框未检查 setonclicklistener 方法不起作用
【发布时间】:2016-12-11 18:10:21
【问题描述】:

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

【问题讨论】:

  • 你试过 setOnCheckedChangeListener();
  • 尝试使用 setOnCheckedChangedListener
  • 我试试这个也行不通:(

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    以这种方式更好地处理和定制。

    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>
    

    【讨论】:

    • @HarpreetSingh 现在有什么问题?
    • 感谢您的工作,我在此视图上方创建了一个视图:|
    【解决方案3】:
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //do stuff
    
        }
    });
    

    【讨论】:

    • 导入 android.widget.CompoundButton;这个或任何其他包?
    • 请试试这个包
    • 并删除你以前使用的包
    猜你喜欢
    • 2015-06-11
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 2016-03-13
    • 2023-03-03
    相关资源
    最近更新 更多