33

Próbuję uzyskać fragment kodu, który powinien skupić EditText w AlertDialog, gdy tylko się wyświetli, a następnie automatycznie otworzy miękką klawiaturę . Zamiast tego sprawia, że ​​ekran staje się ciemniejszy.AlertDialog z EditText, automatyczne otwieranie miękkiej klawiatury z ustawieniem ostrości na EditText nie działa

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(); 

Próbowałem na wiele sposobów, ale nie działało. Mam nadzieję, że możecie mi tutaj pomóc. Z góry dziękuję!

Odpowiedz

59

Ok udało mi się dostać to działa:

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); 

Metoda ta nie potrzebuje dialog, dzięki czemu mogę użyć builder.show(), aby wyświetlić okno dialogowe, a kod udostępniony przez Sabre otwiera klawiaturę. Inny fragment kodu w każdym z przycisków automatycznie zamyka miękką klawiaturę.

+0

NIESAMOWITE !!! Próbowałem dostać klawiaturę, by pojawiła się z moim edittext w okienku alarmowym i nic nie działało. bardzo dziękuję – gbotha

+0

idealne rozwiązanie! – pawegio

+0

Dzięki. Pomogło mi to. –

11

Można to wykorzystać zamiast dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);:

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

nazywają to po dialog.show();

+1

Teraz pokazuje klawiaturą, ale okno nie ... – BlueHazard

+1

była moja odpowiedź przydatne dla ciebie? – Sabre

+0

Ok to działa, ale mogę pokazać kursor na textView. Dowolny pomysł? – adev

0

Spróbuj pokazując je po drugie -

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(); 
    } 

otwarty keyborad w 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); 
    } 
Powiązane problemy