Skip to main content
se eliminaron 28 caracteres en el cuerpo; etiquetas editadas
Origen Enlace
user22539
user22539

public class MainActivity extends AppCompatActivity {

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.button2);
        Button btn2 = (Button) findViewById(R.id.button3);
        Button btn3 = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,thirdActivity.class);
                intent.putExtra("str", "check");
                TextView txt = (TextView) findViewById(R.id.textView2);
                txt.setText("Sin");
                startActivity(intent);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,thirdActivity.class);
                intent.putExtra("str2", "check2");
                TextView txt = (TextView) findViewById(R.id.textView2);
                txt.setText("Con");
                startActivity(intent);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent3 = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent3);
            }
        });

    }
```IntentIntent intent = getIntent();
    String message = intent.getStringExtra("str");
 

    txt = (TextView) findViewById(R.id.textView2);
    txt.setText(""+message);
 

public class MainActivity extends AppCompatActivity {

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

    Button btn = (Button) findViewById(R.id.button2);
    Button btn2 = (Button) findViewById(R.id.button3);
    Button btn3 = (Button) findViewById(R.id.button1);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,thirdActivity.class);
            intent.putExtra("str", "check");
            TextView txt = (TextView) findViewById(R.id.textView2);
            txt.setText("Sin");
            startActivity(intent);
        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,thirdActivity.class);
            intent.putExtra("str2", "check2");
            TextView txt = (TextView) findViewById(R.id.textView2);
            txt.setText("Con");
            startActivity(intent);
        }
    });
    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent3 = new Intent(MainActivity.this,SecondActivity.class);
            startActivity(intent3);
        }
    });

}
```Intent intent = getIntent();
    String message = intent.getStringExtra("str");
 

    txt = (TextView) findViewById(R.id.textView2);
    txt.setText(""+message);
 
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.button2);
        Button btn2 = (Button) findViewById(R.id.button3);
        Button btn3 = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,thirdActivity.class);
                intent.putExtra("str", "check");
                TextView txt = (TextView) findViewById(R.id.textView2);
                txt.setText("Sin");
                startActivity(intent);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,thirdActivity.class);
                intent.putExtra("str2", "check2");
                TextView txt = (TextView) findViewById(R.id.textView2);
                txt.setText("Con");
                startActivity(intent);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent3 = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent3);
            }
        });

    }
Intent intent = getIntent();
String message = intent.getStringExtra("str");

txt = (TextView) findViewById(R.id.textView2);
txt.setText(""+message);
Origen Enlace

¿Cómo capturar dos botones con un único TextView

Tengo dos botones y quiero que al clicar a cada uno me lance una otra actividad, pero mostrando un mensaje diferente implementando un único Text View. No logro hacerlo con un único TextView.

Mi código:

public class MainActivity extends AppCompatActivity {

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

    Button btn = (Button) findViewById(R.id.button2);
    Button btn2 = (Button) findViewById(R.id.button3);
    Button btn3 = (Button) findViewById(R.id.button1);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,thirdActivity.class);
            intent.putExtra("str", "check");
            TextView txt = (TextView) findViewById(R.id.textView2);
            txt.setText("Sin");
            startActivity(intent);
        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,thirdActivity.class);
            intent.putExtra("str2", "check2");
            TextView txt = (TextView) findViewById(R.id.textView2);
            txt.setText("Con");
            startActivity(intent);
        }
    });
    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent3 = new Intent(MainActivity.this,SecondActivity.class);
            startActivity(intent3);
        }
    });

}

En la otra actividad tengo esto:

```Intent intent = getIntent();
    String message = intent.getStringExtra("str");


    txt = (TextView) findViewById(R.id.textView2);
    txt.setText(""+message);