我想检索选中的复选框数量,这是我的尝试。
private int checkCheckBoxes(int total) {
View habitosView = (LayoutInflater.from(mContext)).inflate(R.layout.habitos_list_row, null);
CheckBox CB1 = habitosView.findViewById(R.id.CBdia1);
CheckBox CB2 = habitosView.findViewById(R.id.CBdia2);
CheckBox CB3 = habitosView.findViewById(R.id.CBdia3);
CheckBox CB4 = habitosView.findViewById(R.id.CBdia4);
CheckBox CB5 = habitosView.findViewById(R.id.CBdia5);
CheckBox[] checkBoxes = new CheckBox[5];
checkBoxes[0] = CB1;
checkBoxes[1] = CB2;
checkBoxes[2] = CB3;
checkBoxes[3] = CB4;
checkBoxes[4] = CB5;
int habitosTotal = total;
for (int x=0; x<5; x++){
if(checkBoxes[x].isChecked()){
habitosTotal = habitosTotal + 1;
}
}
Log.d("Habitos total" , habitosTotal+"");
return habitosTotal;
};
对于视图的OnClick我有这个
public void onBindViewHolder(MyViewHolder holder, final int position){
holder.nombre.setText(mHabitosList.get(position).getNombre());
holder.habitosCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int total = mHabitosList.get(position).getTotal();
checkCheckBoxes(total);
AppCompatActivity activity = (AppCompatActivity) view.getContext();
FragmentHabitos fragmentHabitos= new FragmentHabitos();
fragmentHabitos.setArguments(args);
activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentHabitos)
.addToBackStack(null).commit();
}
});
}
无论怎样,它都会返回0,有人可以指出我的错误。我找不到了。
答案 0 :(得分:1)
您应该使用setOnCheckedChangeListener。
样品:
chk.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.checkbox1:
if(isChecked)
//increment checked total
else
//dercrement checked total
break;
case R.id.checkBox2:
if(isChecked)
//increment checked total
else
//decrement checked total
break;
}
}
});
答案 1 :(得分:0)
我在我的应用程序中有它。
btnCadastro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(rb1.isChecked()){
dadosresponsavel = new Dadosresponsavel();
dadosresponsavel.setNome(tNome.getText().toString().trim());
dadosresponsavel.setCidade(tCidade.getText().toString().trim());
dadosresponsavel.setTelefone(tTelefone.getText().toString().trim());
dadosresponsavel.setEmail(useremail.getText().toString().trim());
dadosresponsavel.setSenha(usersenha.getText().toString().trim());
dadosresponsavel.isTipoDeCadastro();
cadastrarResponsavel();
}
else
if(rb2.isChecked()){
Dadosmotorista = new Dadosmotorista();
Dadosmotorista.setNome(tNome.getText().toString().trim());
Dadosmotorista.setCidade(tCidade.getText().toString().trim());
Dadosmotorista.setTelefone(tTelefone.getText().toString().trim());
Dadosmotorista.setEmail(useremail.getText().toString().trim());
Dadosmotorista.setSenha(usersenha.getText().toString().trim());
Dadosmotorista.isTipoDeCadastro();
cadastrarMotorista();
}
}
});