33

Sto cercando di ottenere un pezzo di codice che dovrebbe concentrare un EditText in una AlertDialog non appena viene visualizzato e quindi aprire automaticamente la tastiera virtuale . Invece, rende solo lo schermo più scuro.AlertDialog con EditText, aprire la tastiera virtuale automaticamente con focus su EditText non funziona

Builder builder = new Builder(this); 
final EditText input = new EditText(this); 
AlertDialog dialog = builder.create(); 
builder 
    .setTitle(R.string.dialog_title_addsubject) 
    .setMessage(R.string.dialog_addsubject) 
    .setView(input) 
    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      String value = input.getText().toString(); 
      if (input.getText().toString().trim().length() == 0) { 
       Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show(); 
      } else { 
       db.insertSubject(value); 
       getData(); 
      } 
     } 
    }) 
    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     } 
    }); 
    input.requestFocus(); 
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
    dialog.show(); 

Ho provato molti modi per farlo ma nessuno ha funzionato. Spero che voi ragazzi potete aiutarmi qui. Grazie in anticipo!

risposta

59

Ok sono riuscito a farlo funzionare:

Builder builder = new Builder(this); 
      final EditText input = new EditText(this); 
      builder 
       .setTitle(R.string.dialog_title_addsubject) 
       .setMessage(R.string.dialog_addsubject) 
       .setView(input) 
       .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int which) { 
         String value = input.getText().toString(); 
         if (input.getText().toString().trim().length() == 0) { 
          Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show(); 
         } else { 
          db.insertSubject(value); 
          getData(); 
         } 
         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
         imm.hideSoftInputFromWindow(input.getWindowToken(), 0); 
        } 
       }) 
       .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int which) { 
         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
         imm.hideSoftInputFromWindow(input.getWindowToken(), 0); 
        } 

       }); 

       builder.show(); 
       input.requestFocus(); 
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

Questo metodo non ha bisogno di un dialogo in modo da poter usare builder.show() per mostrare la finestra di dialogo e il codice fornito da Sabre apre la tastiera virtuale. Un altro frammento di codice in ciascuno dei pulsanti chiude automaticamente la tastiera virtuale.

+0

FANTASTICO !!! Ho provato a far comparire la tastiera con il mio edittext nella finestra di avviso e niente ha funzionato. grazie mille – gbotha

+0

soluzione perfetta! – pawegio

+0

Grazie. Mi ha aiutato. –

11

È possibile utilizzare questo invece di dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

chiamata questo dopo dialog.show();

+1

Ora gli spettacoli della tastiera, ma la finestra non lo fa ... – BlueHazard

+1

è stata la mia risposta utile per te? – Sabre

+0

Ok questo lavoro ma posso mostrare il cursore su textView. Qualche idea? – adev

0

Prova mostrando dopo un secondo -

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { 
    input.requestFocus(); 

    dialog.getWindow(). 
    setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

    dialog.show(); 
}, 1000) 
1
public void selectContact(Context context) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setIcon(R.mipmap.icon); 
     builder.setTitle(R.string.title); 
     builder.setPositiveButton(android.R.string.ok, context); 
     builder.setNegativeButton(android.R.string.cancel,context); 
     builder.setView(View.inflate(context, 
       R.layout.dialog, null)); 
     AlertDialog alertDialog = builder.create(); 

     alertDialog.setOnShowListener(this); //Add listener 
     alertDialog.show(); 
    } 

keyborad aperto in OnShow: -

@Override 
    public void onShow(DialogInterface dialog) { 
     EditText editText = (EditText) ((AlertDialog) dialog).findViewById(R.id.number); 
     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 
    } 
Problemi correlati