Android基础-EditText键盘的显示与隐藏

场景一、点击EditText之外的空白区域隐藏键盘:

how to hide soft keyboard on android after clicking outside EditText?

首先定义一个关闭键盘的方法:

    /**
     * 关闭软键盘
     */
    public static void closeSoftKeyboard(Context context) {
        InputMethodManager inputMethodManager = (InputMethodManager)context
            .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null && ((Activity)context).getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(((Activity)context).getCurrentFocus()
                .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

如果用户选择或者点击了输入框以外的区域怎么隐藏掉已经弹出的键盘呢?需要遍历Activity中每个View并检查它是否是EditText的实例,如果不是就注册一个setOnTouchListener,只要点击了该View就会触发它的OnTouch方法,从而在里面隐藏键盘。但实际有更简单的方法可以处理View和ViewGroup,那就是利用下面的递归方法,这个方法其实用处很多,还可以同来设置自定义字体。

public void setupUI(View view) {

    //Set up touch listener for non-text box views to hide keyboard.
    if(!(view instanceof EditText)) {

        view.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard(MyActivity.this);
                return false;
            }

        });
    }

    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {

        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

            View innerView = ((ViewGroup) view).getChildAt(i);

            setupUI(innerView);
        }
    }
}

这样在Activity的setContentViewi之后就能调用上面的setupUI方法了,其中的参数View是Activity的布局的rootView。为Activity的根布局指定一个id就可以了 。

如果想有效的使用该方法,可以把上面的方法放到BaseActivity中,继承它的Activity在onCreate()中调用setupUI就可以了。

场景二、进入带有EditText的Activity时隐藏键盘

这时候一般EditText会获取自动获取焦点然后弹出键盘。进入页面时不自动弹出的话可以:

在EditText的外层layout上设置:

android:focusable="true"

android:focusableInTouchMode="true"

或者强制隐藏Android输入法窗口
例如:  EditText edit=(EditText)findViewById(R.id.edit);  
           InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
           imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

注意:在AndroidMainfest.xml中设置使用EditText的activity的属性,设置windowSoftInputMode属性为adjustUnspecified|stateHidden

场景三、进入带有EditText的Activity时自动弹出键盘

        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // 自动展开键盘
        inputMethodManager.showSoftInput(etUsername, WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

有时候界面还没有完全绘制成功,(这个用Activity的几个生命周期函数是无法判断的,不过可以用onWindowFocusChanged,这个回调函数调用时页面已经加载完毕了),可以适当延迟一定的时间再弹出,比如像下面这样延迟300ms:

    private void initView() {
        btnLogin.setOnClickListener(this);
        tvForgetPassword.setOnClickListener(this);
        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // 自动展开键盘
        autoShowKayboard();
    }

    private void autoShowKayboard() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // 自动展开键盘
                inputMethodManager.showSoftInput(etUsername, WindowManager.LayoutParams
                        .SOFT_INPUT_STATE_VISIBLE);
            }
        }, 300);
    }

 

posted @ 2015-12-01 10:49  Matrix_Ran  阅读(1831)  评论(0编辑  收藏  举报