我有一个带有edittext的片段。 当我点击edittext时,键盘会显示出来。 问题是当我打开抽屉时,抽屉不会隐藏键盘。 即使我切换到另一个片段,键盘仍然显示。 打开抽屉时,如何隐藏键盘。
我试着把
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
和
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
它们都不会隐藏键盘。
答案 0 :(得分:28)
试试这个......
@Override
protected void onCreate(Bundle savedInstanceState) {
......
//Initialize
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
};
快乐的编码......
答案 1 :(得分:26)
请在打开/关闭幻灯片抽屉之前使用此行代码
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
答案 2 :(得分:5)
已弃用抽屉侦听器,您可以使用 mDrawerLayout.addDrawerListener()检测导航抽屉状态更改并关闭键盘onDrawerStateChange
mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
//Called when a drawer's position changes.
}
@Override
public void onDrawerOpened(View drawerView) {
//Called when a drawer has settled in a completely open state.
//The drawer is interactive at this point.
// If you have 2 drawers (left and right) you can distinguish
// them by using id of the drawerView. int id = drawerView.getId();
// id will be your layout's id: for example R.id.left_drawer
}
@Override
public void onDrawerClosed(View drawerView) {
// Called when a drawer has settled in a completely closed state.
}
@Override
public void onDrawerStateChanged(int newState) {
// Called when the drawer motion state changes. The new state will be one of STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
});
答案 3 :(得分:1)
问题是必须从当前“持有”键盘的View中调用getWindowToken()
。这很烦人,我同意你的意见,但这就是它的工作方式。
例如,假设EditText mEditText
是当前焦点接收键盘击键的对象。那么你的代码就是:
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
希望它有所帮助。
答案 4 :(得分:0)
我创建了自己的Helper类来显示或隐藏键盘。
这是...
public static class Helper {
public static void showKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(activity.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
}
}
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null && activity.getCurrentFocus() != null) {
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}
呼叫Helper.showKeyboard(this)
以显示键盘。
调用Helper.hideKeyboard(this)
隐藏键盘。
this
指的是活动。
答案 5 :(得分:0)
这是到目前为止我提出的最简单的最佳解决方案。您要做的就是创建一个InputMethodManager
对象。向DrawerLayout
对象添加一个侦听器,最后
在以下方法inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
中添加代码行,这很高兴。
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float v) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}
@Override
public void onDrawerOpened(@NonNull View view) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}
@Override
public void onDrawerClosed(@NonNull View view) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}
@Override
public void onDrawerStateChanged(int i) {
}
});
答案 6 :(得分:0)
**如果您将切换开关与抽屉一起使用,则在onCreate方法中手动添加onDrawerStateChanged **
DrawerLayout抽屉= findViewById(R.id.drawer_layout); // ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,抽屉,appToolBar,R.string.Open_Drawer,R.string.Close_Drawer);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, appToolBar, R.string.app_name, R.string.app_name) {
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
/* InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
*/ }
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
/* InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
*/ }
@Override
public void onDrawerStateChanged(int newState) {
// Called when the drawer motion state changes. The new state will be one of STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
};
drawer.addDrawerListener(toggle);
toggle.syncState();