1

Estoy incursionando en Android Studio, ¿Cómo podría enviar un texto de un EditText a un TextView sin el uso de un boton? Especificamente, ¿Cómo podría hacer una multiplicación y poder mostrarlo en un TextView con la tecla "Enter" del celular?.

La solución más cercana que he encontrado es esta, pero se me cierra la APP, ocurre un error.

public class MainActivity extends AppCompatActivity implements View.OnKeyListener {

EditText txtCien, txtCincuenta, txtVeinte, txtDiez, txtCinco, txtDos, txtMil;
TextView tvCien, tvCincuenta, tvVeinte, tvDiez, tvCinco, tvDos, tvMil;
Button btn;

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

    txtCien = (EditText) findViewById(R.id.txtCien);
    //tvCien.setText(Integer.parseInt(txtCien.getText().toString())*100000);
    txtCien.setOnKeyListener(this);


}

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if ((event.getAction() == KeyEvent.ACTION_DOWN)&&(keyCode == KeyEvent.KEYCODE_ENTER)){
        Toast.makeText(this, "Pucha", Toast.LENGTH_LONG);
    }
    return false;
}
}

1
  • Con respecto a el error que obtienes siempre acostumbra agregar el error que se muestra en el LogCat, saludos.
    – Jorgesys
    el 21 feb. 2021 a las 21:39

1 respuesta 1

1

Si estas usando un EditText y requieres detectar cuando se da clic en la tecla "Done" del menù contextual, debes usar el listener OnEditorActionListener , cuando detecte la tecla "Done" puedes mostrar el resultado en tu TextView.

    //Obtiene referencia de TextView
    final TextView textView = findViewById(R.id.textView);  

    txtCien = (EditText) findViewById(R.id.txtCien);
    txtCien.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                 //Realiza operaciòn y la escribe en TextView.
                 textView.setText(String.valueOf(2 * 5));

                handled = true;
            }
            return handled;
        }
    });

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.