我有一個片段的活動。讓我們說一個帶有一系列事物的列表片段。現在我想讓用戶添加一個東西,所以我使用FragmentManager將帶有EditText的插入片段替換爲列表片段。 EditText有焦點,光標閃爍。但軟鍵盤無法打開。 其他方式相同:如果用戶輸入新事物並將其添加到列表中,則將插入片段替換爲列表片段。但是,雖然不再有EditText,但鍵盤不會關閉。顯示/隱藏Android軟鍵盤片段替換
什麼是正確的方式來實現呢?我不能相信我必須在所有轉換中手動顯示和隱藏鍵盤?!
我有一個片段的活動。讓我們說一個帶有一系列事物的列表片段。現在我想讓用戶添加一個東西,所以我使用FragmentManager將帶有EditText的插入片段替換爲列表片段。 EditText有焦點,光標閃爍。但軟鍵盤無法打開。 其他方式相同:如果用戶輸入新事物並將其添加到列表中,則將插入片段替換爲列表片段。但是,雖然不再有EditText,但鍵盤不會關閉。顯示/隱藏Android軟鍵盤片段替換
什麼是正確的方式來實現呢?我不能相信我必須在所有轉換中手動顯示和隱藏鍵盤?!
要顯示的鍵盤:
editText.requestFocus();
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(editText, 0);
,然後將其隱藏:
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
我瞭解手動顯示/隱藏鍵盤的方法(儘管其中有些方法並不總是可靠)。但我不相信我必須自己控制這一點。有很多生命週期事件會觸發不同的情況。在分片之前,這是沒有問題的(當從一個Activity到另一個有EditText的Activity時)。所以我希望我仍然不需要手動操作?! – Maniac 2014-09-05 02:19:51
我猜測你ListView
被竊取您的EditText
的焦點,試試這個,讓我知道,如果它有助於。
getListView().setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
一種方法是調用Activity.recreate
,因爲它是必要無論如何處理取向的變化情況。看起來像矯枉過正,而且你最終還得到了不同的過渡動畫。
試試這個
InputMethodManager imgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
editText.requestFocus();
這將肯定工作:
private void hideKeyboard() {
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
private void showKeyboard(){
EditText myEditText = (EditText) findViewById(R.id.myEditText);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
}
我同意,我簡直不敢相信,你不得不手動隱藏鍵盤也是如此。
添加到您的片段的onCreate,你不想讓鍵盤不要在:
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(mToolbar.getWindowToken(), 0);
變化mToolbar佈局內的任何視圖。在這種情況下,我使用我的工具欄。
將此添加到Fragment onCreate,您希望顯示鍵盤。
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
您可以使用下面的代碼隱藏軟鍵盤。
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
我有同樣的問題,我的應用程序,最後我們有2種選擇,首先創建一個通用的功能,並在所有的轉變稱之爲或創建一個全球性的轉變是如何:
public static void RemoveAndReplaceFragment(FragmentManager fragmentManager, int FragmentContent, Fragment PlaceHolderFragment,Activity context){
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragmentManager.findFragmentById(R.id.frgMainActivity))
.commit();
//Close keyBoard in transition
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.animation_fade_in,R.anim.animation_fade_out);
fragmentTransaction.replace(R.id.frgMainActivity, new PlaceholderFragment_MainActivity_AcceptAndFollowTask()).commit();
}
}
的最好的辦法是捕捉過渡事件,但我們不能在這一刻...告訴我,如果我幫你,祝你好運!
我會以以下的事情:
1.擴展Fragment
類
2.覆蓋onAttach()
和onDetach()
回調
3。實現顯示和隱藏軟件鍵盤方法
示例代碼:
class MyFragment extends Fragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//show keyboard when any fragment of this class has been attached
showSoftwareKeyboard(true);
}
@Override
public void onDetach() {
super.onDetach();
//hide keyboard when any fragment of this class has been detached
showSoftwareKeyboard(false);
}
protected void showSoftwareKeyboard(boolean showKeyboard){
final Activity activity = getActivity();
final InputMethodManager inputManager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), showKeyboard ? InputMethodManager.SHOW_FORCED : InputMethodManager.HIDE_NOT_ALWAYS);
}
}
'的EditText上具有焦點,並且光標閃爍。但是軟鍵盤沒有打開.'在你的'AndroidManifest.xml'裏面這個activity的標籤裏,是否設置了'android:windowSoftInputMode =「stateHidden」'?如果是,那麼一旦用戶點擊'EditText',那麼只有軟鍵盤會顯示。即使焦點位於「EditText」上,它也不會顯示。 – 2014-09-04 22:42:44
我認爲你需要參考[android:windowSoftInputMode文件](http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft)並嘗試不同的組合。然後看看你是否可以找到你需要的組合。 – 2014-09-04 22:50:00
我沒有在清單中設置任何輸入模式,我不相信這會有所幫助,因爲它控制軟鍵盤在進入活動時的反應。但我的問題是過渡到另一個片段。 – Maniac 2014-09-05 02:24:34