I would like a Textview to display the amount of Checkboxes that have been marked

Asked

Viewed 128 times

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

inserir a descrição da imagem aqui

Here’s what I’d like to happen exactly.

inserir a descrição da imagem aqui

3 answers

3

If reference to each Checkbox is only required to assign the Listener, can simplify the code by assigning the Listener in the xml.

activity_main.xml

...
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox1" 
    ...
    android:onClick="onCheckBoxClick"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox2" 
    ...
    android:onClick="onCheckBoxClick"/>

...

Mainactivity.java

public class MainActivity extends AppCompatActivity{

    int cont = 0;
    TextView tvContador;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onCheckBoxClick(View view) {
        if (((CheckBox)v).isChecked()) {
            cont++;
            tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));
       else{
            cont--;
            tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));
       }
    }
}
  • I usually do it like this but I wanted to be more conventional today kkk

  • 1

    @Gabriellocalhost The two are valid. The answer I had prepared had the two possibilities, as you replied with a put just this.

2


Buddy, let me give you a hint, you can try to simplify this code. Since they are all checkbox and are basically the same view. Try to make a method that will only count and update Textview, and call it in the checkbox. This will reduce your code and facilitate future fixes and improvements. Try to do this:

public class Teste extends Activity implements View.OnClickListener{

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

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

        cb_01.setOnClickListener(this);
        cb_02.setOnClickListener(this);
        cb_03.setOnClickListener(this);
        cb_04.setOnClickListener(this);
        cb_05.setOnClickListener(this);
        cb_06.setOnClickListener(this);
        cb_07.setOnClickListener(this);
        cb_08.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (((CheckBox)v).isChecked()) {

            cont += 1;
            tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

        } else {

            cont -= 1;
            tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

        }
    }
}

0

I reached the solution shortly after the question, but looked and saw that no one had answered it yet. I decided to show myself how I resolved.

Follows the code:

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

        addCheck();

    }

    private void addCheck() {

        cb_01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()) {

                    cont += 1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                } else {

                    cont -= 1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_04.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_05.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_06.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_07.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_08.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

    }

}

  • Friend, let me give you a hint, Voce can try to simplify this code. Since they are all checkbox and are basically the same view. Try to make a method only that will count and update Textview, and call it in the checkbox. This will reduce your code and facilitate future fixes and improvements.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.