在android中单击隐藏默认键盘

在android中单击隐藏默认键盘,android,Android,我想在屏幕中单击编辑框外侧时隐藏软键盘。如何才能做到这一点?要强制隐藏键盘,请使用以下代码。。。我将它放在一个名为“hideSoftKeyboard()”的方法中。正如Falmari所提到的,当你点击它时,软键盘应该隐藏起来。但是,如果在另一项的“onClick()”中调用此方法,它将强制关闭键盘 private void hideSoftKeyboard(){ if(getCurrentFocus()!=null && getCurrentFocus() instanc

我想在屏幕中单击编辑框外侧时隐藏软键盘。如何才能做到这一点?

要强制隐藏键盘,请使用以下代码。。。我将它放在一个名为“hideSoftKeyboard()”的方法中。正如Falmari所提到的,当你点击它时,软键盘应该隐藏起来。但是,如果在另一项的“onClick()”中调用此方法,它将强制关闭键盘

private void hideSoftKeyboard(){
    if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText){
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(yourEditTextHere.getWindowToken(), 0);
    }
}

我在活动中添加了以下内容。这是因为在可聚焦视图外触摸不会改变焦点(因此w==v),但触摸将在视图的矩形外

public boolean dispatchTouchEvent(MotionEvent event) {
    View v = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);
    View w = getCurrentFocus();
    int scrcoords[] = new int[2];
    w.getLocationOnScreen(scrcoords);
    float x = event.getRawX() + w.getLeft() - scrcoords[0];
    float y = event.getRawY() + w.getTop() - scrcoords[1];

    Log.d("Activity", "Touch event "+event.getRawX()+","+event.getRawY()+" "+x+","+y+" rect "+w.getLeft()+","+w.getTop()+","+w.getRight()+","+w.getBottom()+" coords "+scrcoords[0]+","+scrcoords[1]);
    if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) { 
        inputManager.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
    }
    return ret;

}
公共布尔dispatchTouchEvent(MotionEvent事件){
视图v=getCurrentFocus();
布尔ret=super.dispatchTouchEvent(事件);
视图w=getCurrentFocus();
int scrcoords[]=新int[2];
w、 getLocationOnScreen(scrcoords);
float x=event.getRawX()+w.getLeft()-scrcoords[0];
float y=event.getRawY()+w.getTop()-scrcoords[1];
Log.d(“活动”、“触摸事件”+event.getRawX()+”、“+event.getRawY()+”、“+y+”rect“+w.getLeft()+”、“+w.getTop()+”、“+w.getRight()+”、“+w.getBottom()+”coords“+scrcoords[0]+”、“+scrcoords[1]);
如果(event.getAction()==MotionEvent.ACTION_UP&&(x=w.getRight()| | yw.getBottom()){
inputManager.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(),0);
}
返回ret;
}

[编辑:修复小错误]

必须编辑此错误才能使其正常工作。添加了一个检查,以查看聚焦视图是否为EditText

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

    View v = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);

    if (v instanceof EditText) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];

        Log.d("Activity", "Touch event "+event.getRawX()+","+event.getRawY()+" "+x+","+y+" rect "+w.getLeft()+","+w.getTop()+","+w.getRight()+","+w.getBottom()+" coords "+scrcoords[0]+","+scrcoords[1]);
        if (event.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;
}
@覆盖
公共布尔dispatchTouchEvent(MotionEvent){
视图v=getCurrentFocus();
布尔ret=super.dispatchTouchEvent(事件);
if(v instanceof EditText){
视图w=getCurrentFocus();
int scrcoords[]=新int[2];
w、 getLocationOnScreen(scrcoords);
float x=event.getRawX()+w.getLeft()-scrcoords[0];
float y=event.getRawY()+w.getTop()-scrcoords[1];
Log.d(“活动”、“触摸事件”+event.getRawX()+”、“+event.getRawY()+”、“+y+”rect“+w.getLeft()+”、“+w.getTop()+”、“+w.getRight()+”、“+w.getBottom()+”coords“+scrcoords[0]+”、“+scrcoords[1]);
如果(event.getAction()==MotionEvent.ACTION_UP&&(x=w.getRight()| | yw.getBottom()){
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(),0);
}
}
返回ret;
}

可能会以更平滑的方式完成,但效果非常好。

这可以使用以下代码完成:

1) 使用findviewbyd()将父布局引用到java代码中

2) 然后对其应用setOnTouchListener()

3) 在onTouchMethod()中添加以下代码

公共布尔OutsideTouchEvent(MotionEvent m_事件){
视图v=getCurrentFocus();
布尔值=超级dispatchTouchEvent(m_事件);
视图w=getCurrentFocus();
int scrcoords[]=新int[2];
w、 getLocationOnScreen(scrcoords);
float x=m_event.getRawX()+w.getLeft()-scrcoords[0];
float y=m_event.getRawY()+w.getTop()-scrcoords[1];
如果(m_event.getAction()==MotionEvent.ACTION_UP&&(x=w.getRight()| | yw.getBottom()){
InputMethodManager InputMethodManager=(InputMethodManager)YourActivity.this.getSystemService(Activity.INPUT\u METHOD\u SERVICE);
inputMethodManager.hideSoftInputFromWindow(YourActivity.this.getCurrentFocus().getWindowToken(),0);
}
返回值;
}

将编辑文本的inputType设置为零
editText.setInputType(0)


这是我的工作

首先感谢Daniel,他的代码非常好,我用了一段时间

最近我意识到我必须改进它。问题是滚动页面。我的项目中有许多
EditText
s,当你滚动页面时,它隐藏了键盘

我提出了一个解决方案,使用
OnTestureListener
而不是覆盖
dispatchTouchEvent.

public class TabActivity extends ActionBarActivity implements GestureDetector.OnGestureListener {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        ...
        gestureScanner = new GestureDetector(TabActivity.this,this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        gestureScanner.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onSingleTapUp(MotionEvent event) {
        View v = getCurrentFocus();

        if (v instanceof EditText) {
            View w = getCurrentFocus();
            int scrcoords[] = new int[2];
            w.getLocationOnScreen(scrcoords);
            boolean hide = true;

            View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
            ArrayList<View> editTexts = view.getFocusables(0);     // Get All EditTexts in view

            for(int i=0; i< editTexts.size(); i++){
                View editText = editTexts.get(i);
                editText.getLocationOnScreen(scrcoords);
                float x = event.getRawX();
                float y = event.getRawY();
                int viewX = scrcoords[0];
                int viewY = scrcoords[1];

                // If touch is in any of EditText, keep keyboard active, otherwise hide it.
                if (event.getAction() == MotionEvent.ACTION_UP  && ( x > viewX && x < (viewX + editText.getWidth())) && ( y > viewY && y < (viewY + editText.getHeight())) ) {
                    hide = false;
                }
            }

            if (hide) {
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
            }
        }
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent event, MotionEvent e2, float distanceX, float distanceY) {
        return true;
    }     
}
公共类TabActivity扩展了ActionBarActivity实现了GestureDetector.OnTestureListener{
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
...
...
gestureScanner=新的GestureDetector(TabActivity.this,this);
}
@凌驾
公共布尔dispatchTouchEvent(MotionEvent ev){
手势扫描器。触碰事件(ev);
返回超级dispatchTouchEvent(ev);
}
@凌驾
公共布尔onSingleTapUp(运动事件){
视图v=getCurrentFocus();
if(v instanceof EditText){
视图w=getCurrentFocus();
int scrcoords[]=新int[2];
w、 getLocationOnScreen(scrcoords);
布尔隐藏=真;
视图=((视图组)findViewById(android.R.id.content)).getChildAt(0);
ArrayList EditText=view.getFocusables(0);//获取视图中的所有EditText
对于(int i=0;iviewX&&x<(viewX+editText.getWidth())&&(y>viewY&&y<(viewY+editText.getHeight())){
隐藏=假;
}
}
如果(隐藏){
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
public boolean OutsideTouchEvent(MotionEvent m_event) {
    View v = getCurrentFocus();
    boolean value = super.dispatchTouchEvent(m_event);
    View w = getCurrentFocus();
    int scrcoords[] = new int[2];
    w.getLocationOnScreen(scrcoords);
    float x = m_event.getRawX() + w.getLeft() - scrcoords[0];
    float y = m_event.getRawY() + w.getTop() - scrcoords[1];

    if (m_event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) { 
        InputMethodManager inputMethodManager = (InputMethodManager)  YourActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(YourActivity.this.getCurrentFocus().getWindowToken(), 0);
    }
    return value;

}
public class TabActivity extends ActionBarActivity implements GestureDetector.OnGestureListener {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        ...
        gestureScanner = new GestureDetector(TabActivity.this,this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        gestureScanner.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onSingleTapUp(MotionEvent event) {
        View v = getCurrentFocus();

        if (v instanceof EditText) {
            View w = getCurrentFocus();
            int scrcoords[] = new int[2];
            w.getLocationOnScreen(scrcoords);
            boolean hide = true;

            View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
            ArrayList<View> editTexts = view.getFocusables(0);     // Get All EditTexts in view

            for(int i=0; i< editTexts.size(); i++){
                View editText = editTexts.get(i);
                editText.getLocationOnScreen(scrcoords);
                float x = event.getRawX();
                float y = event.getRawY();
                int viewX = scrcoords[0];
                int viewY = scrcoords[1];

                // If touch is in any of EditText, keep keyboard active, otherwise hide it.
                if (event.getAction() == MotionEvent.ACTION_UP  && ( x > viewX && x < (viewX + editText.getWidth())) && ( y > viewY && y < (viewY + editText.getHeight())) ) {
                    hide = false;
                }
            }

            if (hide) {
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
            }
        }
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent event, MotionEvent e2, float distanceX, float distanceY) {
        return true;
    }     
}
editText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        hideSoftKeyboard();
    }
});
private void hideSoftKeyboard(){
    if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText){
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
}
date.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Hide soft keyboard
       InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

      // here i am showing the Date Dialog. one can proceed with their functionality

            //show date picker dialog
            showDialog(Date_DIALOG_ID);
        }
    });