Create a Alertdialog with an Edittext of type "number" - Android

Asked

Viewed 5,149 times

4

Guys, I have a problem with my alert: In it I have an Edittext, only I can’t declare Edittext with type numbers(android:inputType="number"). Any idea?
Follow the code of the Alert:

public void exibirMensagemEdt(String titulo, String texto){

    AlertDialog.Builder mensagem = new AlertDialog.Builder(TelaCardapio.this);
    mensagem.setTitle(titulo);
    mensagem.setMessage(texto);
    // DECLARACAO DO EDITTEXT
    final EditText input = new EditText(this); 
    mensagem.setView(input);
    mensagem.setNeutralButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {

            Toast.makeText(getApplicationContext(), input.getText().toString().trim(),
                    Toast.LENGTH_SHORT).show();
        }

    });

    mensagem.show();
     // FORÇA O TECLADO APARECER AO ABRIR O ALERT
     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
     imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
  • If I’m not mistaken, just use the method setRawInputType in your Edittext instance.

2 answers

3


You can use the method setRawInputType() as follows:

input.setInputType(InputType.TYPE_CLASS_NUMBER);

Or you can also use the method setInputType() passing the same argument. With this method if the keyboard is already open will restart the keyboard, which does not happen with setRawInputType().

  • Vlw Luidne, it worked here the way you said.

0

final Edittext editQuantidity = new Edittext(this); editQuantidade.setInputType(InputType.TYPE_CLASS_NUMBER);

Browser other questions tagged

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