How to take an entire Edittext value

Asked

Viewed 246 times

-1

I’m having trouble with the method validar() where I want to take an entire amount of the EditText to be able to compare a result in the if. I have already tried casting but does not support the cast of a widget for an entire one. Could anyone help me?

package br.com.irbs.desafiomatematica;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

    public int randomizarNumero(){
        Random random = new Random();

        int numeroRandomico = random.nextInt(100);

        return numeroRandomico;
    }

    public int randomizarNumeros(){
        TextView n1 = findViewById(R.id.numero1);
        TextView n2 = findViewById(R.id.numero2);
        TextView operador = findViewById(R.id.operador);

        int numeroRandomico1 = randomizarNumero();
        int numeroRandomico2 = randomizarNumero();
        int resultado = numeroRandomico1 + numeroRandomico2;

        n1.setText("" + numeroRandomico1);
        n2.setText("" + numeroRandomico2);
        operador.setText("+");

        return resultado;

    }

    public int randomizarNumeros(View view){
        TextView n1 = findViewById(R.id.numero1);
        TextView n2 = findViewById(R.id.numero2);
        TextView operador = findViewById(R.id.operador);

        int numeroRandomico1 = randomizarNumero();
        int numeroRandomico2 = randomizarNumero();
        int resultado = numeroRandomico1 + numeroRandomico2;

        n1.setText("" + numeroRandomico1);
        n2.setText("" + numeroRandomico2);
        operador.setText("+");

        return resultado;
    }

    public void validar(){
        EditText resultado = findViewById(R.id.resultadoUsuario);
        int conta = randomizarNumeros();

        if (resultado == conta) {

        }


    }

}
  • This answers your question? Check if Edittext is empty

  • Although the title is different, the reply accept has a conversion to Double. You are not picking up the text from EditText, read the answer and it will be clearer

1 answer

0

You can use it this way:

int result = Integer.valueOf(seu_edit_text);

Thus converts the value of Edittext to integer numeric, but it is good to define in Edittext to accept only numbers, if it will not cause Exceptions if someone type some letter or special character.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.