2013-08-28 103 views
0

我有第一個活動A用戶設置登錄名和密碼。然後他點擊「確定」,鍵盤打開,他到達活動B.在活動B的onCreate,我顯示一個DialogFrament,但鍵盤仍然打開,直到我隱藏DialogFragment。隱藏鍵盤前顯示DialogFragment

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    GeolocalisationDialog geoDlg = new GeolocalisationDialog(); 
    geoDlg.show(getSupportFragmentManager(), "geoDlg"); 
} 

我的問題是我怎麼能顯示DialogFragment之前隱藏鍵盤?

PS:我DialogFrament是一個基本的DialogFragment

public class GeolocalisationDialog extends DialogFragment { 

private static DialogClickListener callback; 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try { 
     callback = (DialogClickListener) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() + " must implement DialogClickListener"); 
    } 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setMessage(R.string.authorize_geoloc) 
      .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        callback.onYesClick(); 
       } 
      }) 
      .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        callback.onNoClick(); 
       } 
      }); 

    return builder.create(); 
} 

} 

PS2:我曾經試過設置的Android:在清單windowSoftInputMode = 「stateHidden」,它不工作

回答

0

使用以下強行隱藏軟鍵盤:

InputMethodManager imm = (InputMethodManager)getSystemService(
     Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); 
// here you may choose the editText where the last time keyboard was opened. 

請參閱How to make the virtual keyboard go away?瞭解更多信息。

+0

在活動A或B中?我精確地說,如果我沒有顯示DialogFragment,當新活動開始時,鍵盤會自動隱藏 – mrroboaat

+0

如果這似乎造成問題,您可以在'OK'按鈕onClick內執行此操作。我認爲它的用戶完成輸入密碼。所以在第一個活動中做。 'yourEditText'可以是密碼字段的EditText。 –