How to assign values to variables through CheckBoxes and from there to create specific TextViews in another activity

0

I would like values to be assigned to a variable according to the selection of a given CheckBox in my project.

In an example. If the first checkbox was selected, the following variable was assigned: cont+=1.

If the second checkbox was selected, the following variable would be assigned: cont+=2 .

With this in the other activity a condition based on the value of the cont variable would be displayed.

se cont==1 then a textview was created appearing a certain text and se cont==2 appeared another textview with another text:

NowIwouldalsolikethatifyouselectasecondcheckbox(oneofthetwointhebottompart,"employee" or "unemployed"), in the following activity a new textview is created with the following content appearing:

NotethatIcannotuseasingletextviewwiththe2textsjoined(insequence).

Codes:

MainActivity.java

package genesysgeneration.stack;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public int cont=0;
    private Button btnNext;
    private CheckBox cbCasado, cbSolteiro, cbEmpregado, cbDesempregado;

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

        btnNext=(Button)findViewById(R.id.btnNext);
        btnNext.setOnClickListener(this);

    }

    public void onClick(View v){

        Intent it = new Intent(this, Main2Activity.class);
        startActivity(it);

    }

}

Main2Activity.java

package genesysgeneration.stack;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

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

Thanks already!

    
asked by anonymous 08.01.2017 / 22:52

1 answer

0

One more that I discovered the answer shortly after I asked the question, but even after that time all no one answered.

Here is the solution that I found and thought was the best one for my problem:

Main1.java:

package genesysgeneration.stack;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnNext;
    private CheckBox cbCasado, cbSolteiro, cbEmpregado, cbDesempregado;
    private String estadoCivil, estadoEmprego;

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

        cbCasado=(CheckBox)findViewById(R.id.cbCasado);
        cbSolteiro=(CheckBox)findViewById(R.id.cbSolteiro);
        cbEmpregado=(CheckBox)findViewById(R.id.cbEmpregado);
        cbDesempregado=(CheckBox)findViewById(R.id.cbDesempregado);

        addCB();

        btnNext=(Button)findViewById(R.id.btnNext);
        btnNext.setOnClickListener(this);

    }

    private void addCB(){

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

                if (cbSolteiro.isChecked()){

                    cbSolteiro.setChecked(false);
                    estadoCivil="";

                }

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

                    estadoCivil="Casado";

                }else {

                    estadoCivil="";

                }

            }
        });

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

                if (cbCasado.isChecked()){

                    cbCasado.setChecked(false);
                    estadoCivil="";

                }

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

                    estadoCivil="Solteiro";

                }else {

                    estadoCivil="";

                }

            }
        });

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

                if (cbDesempregado.isChecked()){

                    cbDesempregado.setChecked(false);
                    estadoEmprego="";

                }

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

                    estadoEmprego="Empregado";

                }else {

                    estadoEmprego="";

                }

            }
        });

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

                if (cbEmpregado.isChecked()){

                    cbEmpregado.setChecked(false);
                    estadoEmprego="";

                }

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

                    estadoEmprego="Desempregado";

                }else {

                    estadoEmprego="";

                }

            }
        });

    }

    public void onClick(View v){

        Intent it = new Intent(this, Main2Activity.class);
        it.putExtra("estadoCivil", estadoCivil);
        it.putExtra("estadoEmprego", estadoEmprego);
        startActivity(it);

    }

}

I have set a condition so that the user can not select both married and single / employee and unemployed at the same time ... When selecting the second checkbox, if the other checkbox is selected, unchecked:

if (cbSolteiro.isChecked()){

    cbSolteiro.setChecked(false);
    estadoCivil="";

}

Main2.java:

package genesysgeneration.stack;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

    private TextView tv01, tv02;
    private String estadoCivil, estadoEmprego;

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

        tv01=(TextView)findViewById(R.id.tv01);
        tv02=(TextView)findViewById(R.id.tv02);

        estadoCivil=getIntent().getStringExtra("estadoCivil");
        estadoEmprego=getIntent().getStringExtra("estadoEmprego");

        tv01.setText(String.valueOf("O sujeito é " + estadoCivil + "."));
        tv02.setText(String.valueOf("O sujeito é " + estadoEmprego + "."));

    }
}

You must import the two exported strings from the previous activity and ask the TextViews to display them ...

    
10.02.2017 / 01:58