2016-05-13 87 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); 
      } 
});