Create an AlertDialog with an EditText of type "number" - Android

1

Good morning Guys, I have a problem with my Alert: In it I have an EditText, but I can not declare the EditText with type numbers (android: inputType="number"). Any ideas?
Here is the alert code:

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);
}
    
asked by anonymous 18.03.2015 / 04:43

1 answer

2

You can use the setRawInputType() method as follows:

input.setInputType(InputType.TYPE_CLASS_NUMBER);

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

    
18.03.2015 / 18:03