2013-06-21 9 views
34

Mam problem z moim DialogFragment. Aby utworzyć mój widok, używam metody opisanej na blogu na Androida. Oto moja DialogFragmentDialogFragment i force to show keyboard

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    final View myLayout = inflater.inflate(R.layout.dialog_connect, null); 

    edit = (EditText) myLayout.findViewById(R.id.password_edit); 
    edit.requestFocus(); 
    getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

    return myLayout; 
} 

Jeśli używam onCreateView(), to działa, ale chciałbym stworzyć AlterDialog i to zrobić, mam następujący kod:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    builder 
      .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        callback.onYesConnectClick(edit.getText().toString()); 
       } 
      }) 
      .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        callback.onNoConnectClick(); 
       } 
      }); 

    return builder.create(); 
} 

Gdybym skomentować kod z onCreateView(), aplikacja działa, ale nie mogę wymusić pokazania klawiatury i jeśli odkomentuję metodę onCreateView(), wystąpi awaria. Oto ślad stosu:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.ProfileActivity_}: android.util.AndroidRuntimeException:  requestFeature() must be called before adding content 
AndroidRuntime at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2312) 
AndroidRuntime at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2362) 
AndroidRuntime at android.app.ActivityThread.access$600(ActivityThread.java:156) 
AndroidRuntime at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1250) 
AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:99) 
AndroidRuntime at android.os.Looper.loop(Looper.java:137) 
AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5229) 
AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method) 
AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:525) 
AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799) 
AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 
AndroidRuntime at dalvik.system.NativeStart.main(Native Method) 
AndroidRuntime Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 

Więc moje pytanie ==> Czy mogę używać AlertDialog i pokazać klawiaturę, gdy pojawi się okno dialogowe?

Odpowiedz

95

przesłanianie onActivityCreated w dialogfragment i umieścić getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); tam

+3

wtf?! Szukałem tak dużo czasu, a rozwiązanie było takie proste! Jednak jest jedna rzecz, której nie rozumiem. Próbowałem ustawić android: windowSoftInputMode = "stateVisible" w manifeście o działaniu, które jest odpowiedzialne za uruchomienie okna dialogowego i to nie działa ... – mrroboaat

+0

Dziękuję !! Istnieje kilka innych "rozwiązań" tego problemu w StackOverflow i żaden z nich nie działa dla mnie ... ale ten robi. – josh2112

+0

To wydaje się najbardziej skuteczne rozwiązanie spośród wszystkich, jakie mogę znaleźć. Jestem ciekawy, dlaczego to musi być wywołana onActivityCreated, zamiast onResume()? Naprawdę lubię słuchać wyjaśnień. – Sean

12

Odpowiedź tyczj nie działa dla mnie.

Rozwiązaniem okazało, wewnątrz onCreateDialog

Dialog d = builder.create(); 
d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
return d; 

W końcu, kod byłoby tak

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    builder 
      .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        callback.onYesConnectClick(edit.getText().toString()); 
       } 
      }) 
      .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        callback.onNoConnectClick(); 
       } 
      }); 

    Dialog d = builder.create(); 
     d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
return d; 
} 
2

Uważaj na wezwanie setLayout() jeśli używasz go. Zajęło mi trochę czasu uświadomienie sobie, że może on zastąpić atrybuty twojego okna. Po zapakowaniu go do programu obsługi, zaakceptowane rozwiązanie działało dla mnie.

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    final Dialog dialog = super.onCreateDialog(savedInstanceState); 
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 

    return dialog; 
} 

@Override 
public void onStart() { 
    super.onStart(); 

    // without a handler, the window sizes itself correctly 
    // but the keyboard does not show up 
    new Handler().post(new Runnable() { 
     @Override 
     public void run() { 
      getDialog().getWindow().setLayout(DIALOG_WIDTH, DIALOG_HEIGHT); 
     } 
    }); 
} 
+0

idealny !. 'setLayout()' tworzyło problem –

4

Zastosowanie "SOFT_INPUT_STATE_ALWAYS_VISIBLE" zamiast "SOFT_INPUT_STATE_VISIBLE" albo w onActivityCreated lub onCreateDialog metody.

+0

To było rozwiązanie dla mnie w połączeniu z rozwiązaniem Purgarcita w onCreateDialog na końcu. – user960914