Android Studio verify characters from a TextView

0

I am starting with Android studio and I have a problem I want to verify that when entering the name and surnames there are characters but to warn that you have not put anything.

The code is as follows:

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

public class EAC1 extends AppCompatActivity {
    TextView texto;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eac1);
    }

// Métodes dels botons!
    public void ResetTextNom (View vista){ //Hem creat aquest métode que el que fa es que al donar a la X esborri la info.
        TextView TexToResultado = (TextView) findViewById(R.id.TextNom); //fem la variable TextoResultado es canvia per textNom
        TexToResultado.setText(""); // cambia el text actual a un " "
    }
// Ahora vamos hacer el TEXTVIEW.

    public void TextAcceptat (View vista){
        texto = (TextView) findViewById(R.id.TextNom); // Primer verifiquem el text i l'agafem
        if(texto ==null){ // Verifiquem que no estigui buït
            Toast toast1 = Toast.makeText(getApplicationContext(),"Heu d'escriure quelcom!",Toast.LENGTH_LONG);
            toast1.show();
        }else{
            Toast toast2 = Toast.makeText(getApplicationContext(),"bien!",Toast.LENGTH_SHORT);
            toast2.show();
        }
    }}

I have the problem in the final part of the if text = null ...

attachment layout ..

 <EditText
        android:id="@+id/TextNom"
        android:text="@string/name"
        android:layout_width="295dp"
        android:layout_height="49dp"
        android:layout_marginBottom="321dp"
        android:ems="10"
        android:inputType="textAutoComplete|textPersonName"
        android:textColor="@color/colorNegre"
        android:textColorLink="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:ignore="MissingConstraints,RtlHardcoded"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_co

Could it be that I'm missing something in the layout? In the buttons you have to create the android: onclick .. but I do not know if in the texts too ..

thanks!

    
asked by Montse Mkd 24.09.2017 в 20:51
source

1 answer

1

Well let's make a few clarifications.

  • In the layout you have a EditText not a TextView .
  • You are linking the EditText that you declared in the layout with a TextView . You have to link the EditText of the layout with a EditText not with a TextView .
  • You are creating two variables of EditText , one in the method ResetTextNom() and another one in the method TextAcceptat() . So in both methods you are working with two different variables and the changes you make in one will not affect the other. If you modify the value of the variable TexToResultado , the value of the variable texto will remain the same since they are two different variables.

You just have to declare a variable of EditText in the method onCreate() of the class and then you get or modify the value of that variable from the methods ResetTextNom() and TextAcceptat() .

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

public class EAC1 extends AppCompatActivity {

    // Declaras el EditText
    EditText texto;

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

        // Inicializas el EditText
        texto = (EditText) findViewById(R.id.TextNom);
    }

// Métodes dels botons!
    public void ResetTextNom (View vista){ //Hem creat aquest métode que el que fa es que al donar a la X esborri la info.
        //fem la variable TextoResultado es canvia per textNom
        // Modificas el valor del EditText
        texto.setText(""); // cambia el text actual a un " "
    }
// Ahora vamos hacer el TEXTVIEW.

    public void TextAcceptat (View vista){

        // Con getText() obtienes el texto del EditText.
        // Con toString() convierte el texto a String.
        // Con isEmpit() confirma si el EditText esta vacio. Si esta vacio retorna true.
        if(editText.getText().toString().isEmpty()) {
            Toast toast1 = Toast.makeText(getApplicationContext(),"Heu d'escriure quelcom!",Toast.LENGTH_LONG);
            toast1.show();
        }else{
            Toast toast2 = Toast.makeText(getApplicationContext(),"bien!",Toast.LENGTH_SHORT);
            toast2.show();
        }
    }
}
    
answered by 24.09.2017 / 21:27
source