Go to String content of an EditText

1

I am trying to compare the text of a label with that written in a text field, in Android Studio, specifically a TextView and an EditText, I explain:

    if (etInfinitivo.getText().toString().equals(tvVTE.getText().toString()))
    {
        tvVTE.setText("correcto");
    }

Where the variables correspond to:

TextView tvVTE = (TextView) findViewById(R.id.txtVTE);
EditText etInfinitivo = (EditText) findViewById(R.id.txtInfinitivo);

Well, I run the application, I copy the content of the tag (txtVTE) in the text box (txtInfinitivo) and when I press the button, and I get to the first code that I have exposed, if it is not executed, the two chains are not the same.

Can you think of what it may be?

    
asked by José Manuel Ortiz Sánchez 13.04.2018 в 18:45
source

2 answers

1

First of all, the data that is within the EditText will not be taken until the event of Button orders it. For it to work, you must do it within the event setOnClickListener of Button that you have already instantiated.

Example:

TextView tvVTE = (TextView) findViewById(R.id.txtVTE);
EditText etInfinitivo = (EditText) findViewById(R.id.txtInfinitivo);

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

            String palabra1 = tvVTE.getText().toString().trim();
            String palabra2 = etInfinitivo.getText().toString().trim();

            if (palabra1.equalsIgnoreCase(palabra2)) {
                tvVTE.setText("correcto");
            } else {

            }
        }
    });

PD: The equalsIgnoreCase property, as its name says, allows you to ignore the UPPERCASE and lowercase .

    
answered by 13.04.2018 / 19:07
source
2

If the texts are the same in both the TextView and the EditText , there should be no problem when making the comparison.

   if (etInfinitivo.getText().toString().equals(tvVTE.getText().toString()))
    {
        tvVTE.setText("correcto");
    }

But imagine that you added a space or that in one of the views you entered a different uppercase or lowercase letter to the other text!

I suggest you use 2 methods to avoid this kind of problems:

  

trim () Return a copy of the chain, with the blank   initial and final omitted.

and

  

toLowerCase () Convert all characters of this Chain in   lowercase using the rules of the locale   default.

Now apply to your if sentence:

   if (etInfinitivo.getText().toString().equals(tvVTE.getText().toString()))
    {
        tvVTE.setText("correcto");
    }

in this way you will avoid blank spaces or characters that are upper or lower in the comparison.

    
answered by 13.04.2018 в 19:46