我在我的应用程序中添加了导航抽屉。到目前为止其他一切工作都很顺利,但我遇到的问题是导航抽屉打开时键盘没有关闭。导航抽屉是主要活动,然后从抽屉打开的每个页面都是一个片段。
我尝试将以下内容添加到片段中的每个EditText区域。但是,这并没有结束任何事情。
InputMethodManager imm1 = (InputMethodManager)getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm1.hideSoftInputFromWindow(input1.getWindowToken(), 0);
我也尝试将该代码放在主要活动中,但也没有成功。关于我可以采用哪些不同的方法来实现这一目标的任何想法?
答案 0 :(得分:14)
要在打开或关闭导航栏时隐藏打开的键盘,请覆盖onDrawerSlide
中的方法onDrawerListner
并添加以下行
InputMethodManager inputMethodManager = (InputMethodManager) actionBarActivity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
actionBarActivity.getCurrentFocus().getWindowToken(),
0
);
答案 1 :(得分:9)
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
};
drawer.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
答案 2 :(得分:4)
这对我很有用:
private ActionBarDrawerToggle aDrawerToggle;
private DrawerLayout aDrawerLayout;
我在创建类后在void中使用此代码:
aDrawerToggle = new ActionBarDrawerToggle(getActivity(),
aDrawerLayout,
R.drawable.main_icon,
R.string.drawer_open,
R.string.drawer_close){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
InputMethodManager inputMethodManager = (InputMethodManager)getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
//inputSearch is my EditText
inputMethodManager.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
getActivity().supportInvalidateOptionsMenu();
}
}
答案 3 :(得分:3)
在DrawerLayout中添加DrawerListener。然后,您可以使用上面的代码在onDrawerOpened()方法中关闭键盘
答案 4 :(得分:1)
on onCreate:
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
});
答案 5 :(得分:0)
尝试此代码,我在我的应用程序中使用它,例如,如果我打开一个包含EditText的对话框 我在create.Hope上设置了这个代码,这有助于
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
答案 6 :(得分:0)
This does not solve issue.
Correct solution is: Override below method on your main activity:
public boolean dispatchTouchEvent(MotionEvent ev)
{
View view = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(ev);
if(drawer_open) {
//如果滑块打开则不执行任何操作 } 其他 { if(查看instanceof EditText){ 查看w = getCurrentFocus(); int scrcoords [] = new int [2]; w.getLocationOnScreen(scrcoords); float x = ev.getRawX()+ w.getLeft() - scrcoords [0]; float y = ev.getRawY()+ w.getTop() - scrcoords [1];
if (ev.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return ret;
}
答案 7 :(得分:0)
自此以来,不赞成使用canvas.setDrawerListener(toggle)。如下所述使用抽屉.addDrawerListener(toggle)
private void initDrawerLayout(){
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle
(this, drawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close)
{
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
};
drawer.addDrawerListener(toggle);
toggle.syncState();
drawer.closeDrawer(GravityCompat.START);
}