我试图在膨胀LinearLayout并调用setContentView之后显示键盘:
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(etContent, InputMethodManager.SHOW_FORCED);
getContent.requestFocus();
Run Code Online (Sandbox Code Playgroud)
它没用.我也试过这个:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Run Code Online (Sandbox Code Playgroud)
但它也没有用.如何强制键盘显示/隐藏?我做错了什么?
sli*_*n77 34
这应该工作
public class KeyBoard {
public static void toggle(Activity activity){
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm.isActive()){
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide
} else {
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); // show
}
}//end method
}//end class
Run Code Online (Sandbox Code Playgroud)
此链接清楚地说明了隐藏软键盘。要显示它,您可以使用 hack - 在布局中的任何位置创建一个 EditText,layout_width 和 layout_height=0dip,并在 onCreate 中执行
yourEditText.requestFocus();
Run Code Online (Sandbox Code Playgroud)