当按钮点击时隐藏键盘(片段)
当按钮点击时,我得到错误来实现隐藏键盘,任何人都知道如何解决这个问题? 在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) {
..........
}
您使用Fragment
这么写的像getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)
原因是一样的:
活动扩展了上下文,片段没有。因此,你首先需要获得在该片段包含
编辑
你在注释中提到的其他错误活动的参考,您可以使用
getView().getWindowToken()
和隐藏方法应该在你的button's
onClick()
方法中调用
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
和'getWindowsToken()'?警告说:不能从一个静态的上下文引用 –
你可以使用'getView()。getWindowToken()' –
感谢它的工作:) :) –
使用下面的代码
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();
}
// 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);
}
}
使用此,
public static void hideKeyboard(Context mContext) {
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(((Activity) mContext).getWindow()
.getCurrentFocus().getWindowToken(), 0);
}
hitung.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
});
我们能不能得到任何栈打印,在logcat的错误代码? – Amesys
嗨,即时通讯还没有运行,仍然出现bc错误,上getSystemService警告说:'不能解决方法getSystemService(Java.Lang.String)' –
是缺少'('在这一行中的一个错误?InputMethodManager imm = InputMethodManager)getSystemService (Context.INPUT_METHOD_SERVICE); –