簡體   English   中英

在片段替換時顯示/隱藏Android軟鍵盤

[英]Show / hide Android softkeyboard on fragment replace

我有一個片段活動。 讓我們說一個包含事物列表的列表片段。 現在我想讓用戶添加一個東西,所以我使用FragmentManager用一個帶有EditText的插入片段替換列表片段。 EditText具有焦點,光標閃爍。 但軟鍵盤無法打開。 反之亦然:如果用戶輸入了新內容並將其添加到列表中,我將插入片段替換為列表片段。 但是,雖然沒有EditText,鍵盤也不會關閉。

實現這個的正確方法是什么? 我不敢相信我必須在所有過渡時手動顯示和隱藏鍵盤?!

我想跟隨的事情:

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

我的app有同樣的問題,最后我們有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();
    }
}

最好的方法是捕捉過渡事件,但我們不能在這一刻......告訴我,如果我幫助你,祝你好運!

這肯定會起作用:

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

我猜你的ListView正在竊取你的EditText的焦點,嘗試這個,讓我知道它是否有幫助。

getListView().setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

嘗試這個

InputMethodManager imgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

    imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    editText.requestFocus();

我同意,我不敢相信你也必須手動隱藏鍵盤。

將此添加到Fragment 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);

一種方法是調用Activity.recreate ,因為無論如何都需要處理方向改變的情況。 看起來像矯枉過正,你最終也會得到不同的過渡動畫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM