"Go" Android click on an empty area, hide Input Method soft keyboard

Source: Internet
Author: User
Tags gety

Original URL: http://www.2cto.com/kf/201505/401382.html

Many times, when we use the application, there will be an input method soft keyboard pop-up problem, usually, we will let the user click the Return button or the next step to hide the soft keyboard. For a better experience, we can implement when the user finishes using the soft keyboard. Click on an empty area to achieve hidden functionality. Effect:

Code implementation

code block syntax follows the standard markdown code, for example:

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273 <code class="language-java" hljs="">package example.com.jinlin.myapplication; import android.content.Context;import android.os.Bundle;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.view.MotionEvent;import android.view.View;import android.view.inputmethod.InputMethodManager;import android.widget.EditText; /** * Created by J!nl!n on 15/5/21. */public abstract class BaseActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        iniView();    }    public abstract void iniView();    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        if (ev.getAction() == MotionEvent.ACTION_DOWN) {            View v = getCurrentFocus();            if (isShouldHideKeyboard(v, ev)) {                hideKeyboard(v.getWindowToken());            }        }        return super.dispatchTouchEvent(ev);    }    /**     * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时则不能隐藏     *     * @param v     * @param event     * @return     */    private boolean isShouldHideKeyboard(View v, MotionEvent event) {        if (v != null && (v instanceof EditText)) {            int[] l = {0, 0};            v.getLocationInWindow(l);            int left = l[0],                top = l[1],                bottom = top + v.getHeight(),                right = left + v.getWidth();            if (event.getX() > left && event.getX() < right                    && event.getY() > top && event.getY() < bottom) {                // 点击EditText的事件,忽略它。                return false;            } else {                return true;            }        }        // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditText上,和用户用轨迹球选择其他的焦点        return false;    }    /**     * 获取InputMethodManager,隐藏软键盘     * @param token     */    private void hideKeyboard(IBinder token) {        if (token != null) {            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);        }    }}</code>

Of course we have a simpler way to implement this function, just rewrite the Ontouchevent method. The code is as follows:

?
1234567891011 // 点击空白区域 自动隐藏软键盘    public boolean onTouchEvent(MotionEvent event) {        if(null != this.getCurrentFocus()){            /**             * 点击空白位置 隐藏软键盘             */            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);        }        return super .onTouchEvent(event);    }

Use a baseactivity to do some common operations, other activity is inherited from the base class activity, then all the interface can be realized by clicking on the blank area, hide the soft keyboard.

Android Click to close the soft keyboard

Original URL: http://www.2cto.com/kf/201412/360428.html

In the project, the EditText will automatically eject the soft keyboard when the focus is taken, and when you close it, you usually need to press the back key or click the button on the soft keyboard.

Even if the current activity has been completed, the soft keyboard still exists, affecting the user's experience.

Online There are many very detailed methods, such as clicking on other blank areas, the soft keyboard will disappear and the like, we do not require this project, the requirements are as long as

Do not block other operations, and the current activity is closed after the soft keyboard disappears on the line,

Today we share two ways:

?
1234567891011 //This method, if the display is hidden, if hidden then display private void hintkbone () { inputmethodmanager IMM = (Inputmethodmanager) getactivity (). Getsystemservice ( Context.input_method_service);    //get Inputmethodmanager instance if (imm.isactive ()) { &NBSP; //if you turn on imm.togglesoftinput (inputmethodmanager.show_implicit, Inputmethodmanager.hide_not_always); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   &NBSP;&NBSP;&NBSP;&NBSP; } }

?
123456789 //此方法只是关闭软键盘private void hintKbTwo() { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);             if(imm.isActive()&&getCurrentFocus()!=null){    if (getCurrentFocus().getWindowToken()!=null) {    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);    }              }}


Just call the method when you need to click on the event to close the soft keyboard.

"Go" Android click on an empty area, hide Input Method soft keyboard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.