Android - problema con TextView

 
Vista:
Imágen de perfil de Charly
Val: 123
Bronce
Ha aumentado su posición en 2 puestos en Android (en relación al último mes)
Gráfica de Android

problema con TextView

Publicado por Charly (69 intervenciones) el 01/10/2016 13:31:52
Hola, soy nuevo en esto de Android y me han mandado una pagina para que meta un numero y mi nombre y al darle a un boton me aparezca debajo en un TextView la frase: "Hola "+nombre+", te gusta el número "+numero+"?"
Tengo hecho casi todo, pero me lio al mandarle la operación al botón en el archivo .java.
A continuación pongo el código del layout y después del java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView android:id="@+id/lblEtiqueta1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Introduce un numero:"
        android:background="#AA44FF"
        android:typeface="monospace" />
    <EditText android:id="@+id/txtTexto1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:text=""
        android:layout_below="@id/lblEtiqueta1" />
    <TextView android:id="@+id/lblEtiqueta2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Introduce tu nombre:"
        android:background="#AA44FF"
        android:typeface="monospace" />
    <EditText android:id="@+id/txtTexto2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:text=""
        android:layout_below="@id/lblEtiqueta2" />
    <Button android:id="@+id/BtnSaludar"
        android:text="Saludar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="PulsaBoton" />
    <TextView android:id="@+id/lblEtiqueta3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:background="#AA44FF"
        android:typeface="monospace" />
</LinearLayout>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package charly.izquierdofp.es.curso;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class TextoActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_texto);
    }
    public void PulsaBoton(View view) {
        //view representa el botón pulsado
        Button btn1 = (Button)findViewById(R.id.BtnSaludar);
        EditText txtTexto1 = (EditText)findViewById(R.id.txtTexto1);
        String texto1 = txtTexto1.getText().toString();
        EditText txtTexto2 = (EditText)findViewById(R.id.txtTexto2);
        String texto2 = txtTexto2.getText().toString();
        String texto3 = "Hola "+texto2+", te gusta el número "+texto1+"?";
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 444
Oro
Ha mantenido su posición en Android (en relación al último mes)
Gráfica de Android

problema con TextView

Publicado por juanki (253 intervenciones) el 01/10/2016 17:20:48
Hola

No indicas cual es el error o la duda exacta que tienes. Te comento lo que vi por encima.

1
2
3
4
5
6
7
8
9
10
public void PulsaBoton(View view) {
        // Esto sobra
//        Button btn1 = (Button)findViewById(R.id.BtnSaludar);
 
        EditText txtTexto1 = (EditText)findViewById(R.id.txtTexto1);
        String texto1 = txtTexto1.getText().toString();
        EditText txtTexto2 = (EditText)findViewById(R.id.txtTexto2);
        String texto2 = txtTexto2.getText().toString();
        String texto3 = "Hola "+texto2+", te gusta el número "+texto1+"?";
    }

En el método que responde a la pulsación del botón no tienes que obtener una referencia a el, simplemente se ejecutará ese código cuando lo pulses, ya que lo definiste en el XML.

Te falta obtener la referencia al TextView en el que quieras poner el mensaje y ponerlo.

¿Cuales son los problemas que tienes?

Un saludo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
2
Comentar