在我的对话框片段中,我可以使用以下方式显示键盘:
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT STATE_VISIBLE);
但我无法在dismiss
上隐藏它。
我已经尝试过
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
和
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
两个都不起作用。
我还尝试了使用
</p><input type="text" autofocus=""><p>
显示和隐藏键盘。InputMethodManager inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);
并且
InputMethodManager inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
但是这些无法显示或隐藏键盘。
public static class MyDialogFragment extends DialogFragment
{
@Nullable @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.my_input_dialog, container, false);
}
@Override
public void onViewCreated(View v, Bundle savedInstanceState)
{
super.onViewCreated(v, savedInstanceState);
final EditText editText = (EditText)v.findViewById(R.id.input);
// this line below is able to show the keyboard
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Button add = (Button)v.findViewById(R.id.add_btn);
add.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// other code not shown
dismiss();
}
});
Button cancel = (Button)v.findViewById(R.id.cancel_btn);
cancelButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dismiss();
}
});
}
@Override
public void onDismiss(DialogInterface dialog)
{
//this line below does NOT work, it does not hide the keyboard
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
super.onDismiss(dialog);
}
}
注意:我已阅读这些 stackoverflow 帖子,并尝试了提出的解决方案,但都没有成功: