2013-06-21 93 views
34

我的DialogFragment出現問題。因此,爲了創建我的視圖,我使用了android博客中描述的方法。 這裏是我的DialogFragmentDialogFragment和強制顯示鍵盤

@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; 
} 

如果我使用onCreateView(),它的工作原理,但我想創造一個AlterDialog而要做到這一點,我有以下代碼:

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

如果我評論了代碼從onCreateView(),該應用程序的作品,但我不能強制鍵盤顯示,如果我取消onCreateView(),我得到一個崩潰。 這裏是堆棧跟蹤:

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 

所以我的問題==>我可以使用AlertDialog和顯示鍵盤出現的對話框的時候?

回答

95

覆蓋onActivityCreated在dialogfragment並把getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);在那裏

+3

wtf?!我期待了這麼多時間,解決方案非常簡單!然而,有一件事我不明白。我試圖在清單中設置android:windowSoftInputMode =「stateVisible」,它是負責啓動對話框的活動,它不起作用... – mrroboaat

+0

謝謝!在StackOverflow上有大約一打「解決方案」來解決這個問題,並且它們都不適合我......但是這樣做。 – josh2112

+0

這似乎是我所能找到的最有效的解決方案。我很好奇爲什麼必須在onActivityCreated而不是onResume()中調用?我非常喜歡聽到一些解釋。 – Sean

12

tyczj的答案對我不起作用。

的解決方案是,裏面onCreateDialog

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

最後,該代碼會是這樣

@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

當心setLayout()呼叫如果你使用它。我花了一段時間才意識到它可能會覆蓋您窗口的屬性。在將其包裝到處理程序後,接受的解決方案爲我工作。

@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

只是完美! 'setLayout()'產生了這個問題 –

4

使用"SOFT_INPUT_STATE_ALWAYS_VISIBLE"代替"SOFT_INPUT_STATE_VISIBLE"無論是在onActivityCreatedonCreateDialog方法。

+0

這是我在最後與onCreateDialog中的Purgarcita解決方案結合使用的解決方案。 – user960914