繁体   English   中英

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

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

我需要找出一种方法来检索使用下面列出的代码创建的复选框的选中状态:

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

任何指导将不胜感激。

我认为您必须使用for循环来检查是否选中了这样的复选框

cb.isChecked()==true;

对于每个复选框,请使用Integer数组,将“ 0”保存为true,将“ 1”保存为false。

您可以将其保存为文件,SharedPrefrences或CoreData,以便下次运行该应用程序时将保留相同的值

您可以像这样检查您的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
}

签入文件:

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

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

此代码使您可以存储复选框的ID。 我开发了迈克尔·施密特的代码。 非常感谢,迈克尔!

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

尝试这个:

 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