0
I made several attempts, but I was not successful, here is the code of Mainactivity.java:
package genesysgeneration.cbnumber;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
CheckBox cb_01, cb_02, cb_03, cb_04, cb_05, cb_06, cb_07, cb_08;
int cont = 0;
TextView tvContador;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb_01=(CheckBox)findViewById(R.id.checkBox1);
cb_02=(CheckBox)findViewById(R.id.checkBox2);
cb_03=(CheckBox)findViewById(R.id.checkBox3);
cb_04=(CheckBox)findViewById(R.id.checkBox4);
cb_05=(CheckBox)findViewById(R.id.checkBox5);
cb_06=(CheckBox)findViewById(R.id.checkBox6);
cb_07=(CheckBox)findViewById(R.id.checkBox7);
cb_08=(CheckBox)findViewById(R.id.checkBox8);
tvContador=(TextView)findViewById(R.id.tvContador);
tvContador.setText(String.valueOf(cont));
}
public void onCheckboxClicked (View view){
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId()){
case R.id.checkBox1:
if(checked)
cont+=1;
break;
case R.id.checkBox2:
if(checked)
cont+=1;
break;
case R.id.checkBox3:
if(checked)
cont+=1;
break;
case R.id.checkBox4:
if(checked)
cont+=1;
break;
case R.id.checkBox5:
if(checked)
cont+=1;
break;
case R.id.checkBox6:
if(checked)
cont+=1;
break;
case R.id.checkBox7:
if(checked)
cont+=1;
break;
case R.id.checkBox8:
if(checked)
cont+=1;
break;
}
}
}
What happens
Here’s what I’d like to happen exactly.
I usually do it like this but I wanted to be more conventional today kkk
– GabrielLocalhost
@Gabriellocalhost The two are valid. The answer I had prepared had the two possibilities, as you replied with a put just this.
– ramaral