2016-05-13 89 views
2

當按鈕點擊時,我得到錯誤來實現隱藏鍵盤,任何人都知道如何解決這個問題? 在getSystemService和getWindowsToken實際誤碼當按鈕點擊時隱藏鍵盤(片段)

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_calculator, container, false); 

     Button hitung = (Button) rootView.findViewById(R.id.hitung); 
     final EditText height = (EditText)rootView.findViewById(R.id.height); 
     final EditText weight = (EditText)rootView.findViewById(R.id.weight); 

     InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(EditText.getWindowToken(), 0); 

     final TextView result = (TextView)rootView.findViewById(R.id.result); 
     final TextView finalresult = (TextView)rootView.findViewById(R.id.finalresult); 
     finalresult.setMovementMethod(new ScrollingMovementMethod()); 

     hitung.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      .......... 
} 
+0

我們能不能得到任何棧打印,在logcat的錯誤代碼? – Amesys

+0

嗨,即時通訊還沒有運行,仍然出現bc錯誤,上getSystemService警告說:'不能解決方法getSystemService(Java.Lang.String)' –

+0

是缺少'('在這一行中的一個錯誤?InputMethodManager imm = InputMethodManager)getSystemService (Context.INPUT_METHOD_SERVICE); –

回答

5

您使用Fragment這麼寫的像getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)

原因是一樣的:

活動擴展了上下文,片段沒有。因此,你首先需要獲得在該片段包含

編輯

你在註釋中提到的其他錯誤活動的參考,您可以使用

getView().getWindowToken()

和隱藏方法應該在你的button'sonClick()方法中調用

imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);

+0

和'getWindowsToken()'?警告說:不能從一個靜態的上下文引用 –

+0

你可以使用'getView()。getWindowToken()' –

+0

感謝它的工作:) :) –

1

使用下面的代碼

try { 
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 
          InputMethodManager.HIDE_NOT_ALWAYS); 
       } catch (Exception e) { 
        if(net.one97.paytm.common.utility.CJRAppCommonUtility.isDebug) e.printStackTrace(); 
       } 
1
// hide keyboard 
public static void hideSoftKeyboard(Context context, View view) { 
    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE); 

    if(inputMethodManager != null && inputMethodManager.isActive()) 
    { 
     //inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); 
     //InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    } 
} 
5

使用此,

public static void hideKeyboard(Context mContext) { 
    InputMethodManager imm = (InputMethodManager) mContext 
      .getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(((Activity) mContext).getWindow() 
      .getCurrentFocus().getWindowToken(), 0); 
} 
+0

嗯,它像我的鍵盤永遠隱藏從我開始我的應用程序 –

+0

調用此方法按鈕點擊。它的工作和測試。 @F_X – Tejas

+0

@F_X:如果對你有幫助,請接受我的回答。 – Tejas

1
hitung.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
      } 
});