9

i have this problem. I have create a small app Android.

I show a AlertDialog.Builder with EditText, so the user must click on the EdiText, select Number 123 then insert a int number.

I would like to show a keyboard with only number. Can we Help me? It's possible to create a AlertDialog with automaticaly focus?

I have write 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();
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
bircastri
  • 2,169
  • 13
  • 50
  • 119

3 Answers3

21

This code is what you need. Just insert it wherever you need to launch the alert dialog. I haven't figured out how to launch the keyboard automatically , but it shouldn't 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();
Josh
  • 6,251
  • 2
  • 46
  • 73
1

You need to add this line:

inputText.setRawInputType(Configuration.KEYBOARD_12KEY);
Mikel
  • 1,581
  • 17
  • 35
  • Thanks all, now found. But if i would to show the keyboard automatically?? Now when i show the Alert i must click on inputText then show keyboard. I have put this code but not found: inputText.setFocusable(true); – bircastri Oct 16 '13 at 14:40
-1
enter code here

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.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) 
              {
              


              }
            });
            alert.show();