How to insert editText number in Android AlertDialog

I have this problem. I created a small Android application.

I am showing AlertDialog.Builder with EditText, so the user must click on EdiText, select Number 123, and then paste the number int.

I would like to show a keyboard with a single number. Can we help me? Is it possible to create an AlertDialog with auto focus?

I wrote this code. Can we help me?

AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle("Inserisci quantità");
                alert.setMessage("Inserisci una quantità per l'articolo: "+articolo.getNomeArticolo());
                final EditText inputText = new EditText(this);
                alert.setView(inputText);
                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                                String value = inputText.getText().toString();
                                try{
                                        int quantita = Integer.parseInt(value);
                                        ArticoliOrdine articoloOrdine = new ArticoliOrdine();
                                        articoloOrdine.setIdArticolo(articolo.getCodArticolo());
                                        articoloOrdine.setNomeArticolo(articolo.getNomeArticolo());
                                        articoloOrdine.setQuantia(quantita);
                                        listaArticoli.add(articoloOrdine);

                                        adapter.notifyDataSetChanged();
                                }catch(Exception e){
                                        AlertDialog.Builder alertErrore = new AlertDialog.Builder(getApplicationContext());
                                        alertErrore.setTitle("Errore");
                                        alertErrore.setMessage("Hai inserito una quantità non valida.");
                                        alertErrore.show();

                                }

                        }
                });


                // Showing Alert Message
                alert.show();
+4
source share
2 answers

You need this code. Just paste it where you need to run the warning dialog. I did not understand how to start the keyboard automatically, but it should not be difficult.

AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle(multiLangTranslation(R.string.manualshippermessage));
                final EditText input = new EditText(this);
                input.setInputType(InputType.TYPE_CLASS_NUMBER);
                input.setRawInputType(Configuration.KEYBOARD_12KEY);
                alert.setView(input);  
                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  //Put actions for OK button here
                  }
                });
                alert.setNegativeButton(multiLangTranslation(R.string.cancel), new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                      //Put actions for CANCEL button here, or leave in blank
                  }
                });
                alert.show();
+10
source

:

inputText.setRawInputType(Configuration.KEYBOARD_12KEY);
+1

All Articles