[英]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.