我已经放置了复选框,我想选择放在android中的表格布局中的所有复选框但它对我不起作用任何人都可以告诉我如何执行此操作这是我选择所有的代码表格布局中的复选框:
public class Manual_AC_Fuse_ckt extends Fragment {
public int []check_box_count = new int[12];
CheckBox check_box,
check_box_count[0] = R.id.cb_1;
check_box_count[1] = R.id.cb_2;
check_box_count[2] = R.id.cb_3;
check_box_count[3] = R.id.cb_4;
check_box_count[4] = R.id.cb_5;
check_box_count[5] = R.id.cb_6;
check_box_count[6] = R.id.cb_7;
check_box_count[7] = R.id.cb_8;
check_box_count[8] = R.id.cb_9;
check_box_count[9] = R.id.cb_10;
check_box_count[10] = R.id.cb_11;
check_box_count[11] = R.id.cb_12;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Root_view = inflater.inflate(R.layout.manualmode_ac_fuse_ckt, null);
table = (TableLayout)Root_view.findViewById(R.id.table_layout_manual_mode);
registerForContextMenu(table);
return Root_view;
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("OPTIONS");
menu.add(0, v.getId(), 0, "SELECT ALL");
menu.add(0, v.getId(), 0, "DESELECT ALL");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="SELECT ALL"){
for(i=0;i<table.getChildCount();i++){
check_box_2 = (CheckBox)Root_view.findViewById(check_box_count[i]);
check_box_2.setChecked(true);
}
}
return super.onContextItemSelected(item);
}
}
答案 0 :(得分:1)
试试这个。
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle().equals("SELECT ALL")){
for(i=0;i<table.getChildCount();i++){
check_box_2 = (CheckBox)Root_view.findViewById(check_box_count[i]);
check_box_2.setChecked(true);
}
}
return super.onContextItemSelected(item);
}