简体   繁体   English

如何检查Android应用程序中的复选框是否已选中?

[英]How to check if checkbox is checked or unchecked in android application?

I need to find out a way how to retrieve checked state for checkboxes created with code listed below: 我需要找出一种方法来检索使用下面列出的代码创建的复选框的选中状态:

for (int i = 0; i < cnt; i++) {
                    cb = new CheckBox(getApplicationContext());
                    TextView txt = new TextView(getApplicationContext());
                    final LinearLayout ll2 = new LinearLayout(
                            PollActivity.this);
                    ll2.setOrientation(LinearLayout.HORIZONTAL);
                    ll2.addView(txt);
                    ll2.addView(cb);
                    txt.setText(PersianReshape
                            .reshape(last_poll_array[i][1]));
                    txt.setTypeface(face);
                    ll.setGravity(Gravity.RIGHT);
                    ll2.setGravity(Gravity.RIGHT);
                    ll.addView(ll2);

                }
                sc.addView(ll);

Any guidance will be appreciated. 任何指导将不胜感激。

I think you have to use a for loop to check whether a checkbox is checked or not like this 我认为您必须使用for循环来检查是否选中了这样的复选框

cb.isChecked()==true;

and for every checkbox use a Integer array save the value like "0" for true and "1" for false. 对于每个复选框,请使用Integer数组,将“ 0”保存为true,将“ 1”保存为false。

You can save this is file, SharedPrefrences or CoreData so that next time you run the application it'll hold the same values 您可以将其保存为文件,SharedPrefrences或CoreData,以便下次运行该应用程序时将保留相同的值

You could check the children of you ll2 like this: 您可以像这样检查您的ll2的孩子:

boolean oneChecked = false;
View v = null;
for(int i=0; i<ll2.getChildCount(); i++) {
    v = ll2.getChildAt(i);
    if (v instanceof CheckBox) {
        if (((CheckBox) v).isChecked()) {
            oneChecked = true;
            break;
        }
    }
}
if (oneChecked) {
    // Do whatever you like if one CheckBox is checked
}

Check in the documents: 签入文件:

if (checkBox.isChecked()) {
             checkBox.setChecked(false);
         }

check out this link: https://developer.android.com/reference/android/widget/CheckBox.html 查看此链接: https : //developer.android.com/reference/android/widget/CheckBox.html

This code enables you to store ids of the checkBox. 此代码使您可以存储复选框的ID。 I developed Michael Schmidt's codes. 我开发了迈克尔·施密特的代码。 Thanks a lot, Michael!! 非常感谢,迈克尔!

ArrayList<Integer> check = new ArrayList<Integer>();

for(int k=0;k<ll.getChildCount();k++){
            View v = ll.getChildAt(k);
            if(v instanceof CheckBox){
                if(((CheckBox)v).isChecked()){
                    check.add(ll.getChildAt(k).getId());
                }
            }
        }

Try this: 尝试这个:

 if(chckbxcuger.isSelected())rezultatas.setText("cuger");
    if(chckbxmilk.isSelected())rezultatas.setText("milk"); 
    if(chckbxCukrus.isSelected()&&chckbxPienas.isSelected())rezultatas.setText("cuger and milk");

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

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