Android开发 输入法调用学习

方法一(若是输入法在窗口上已经显示,则隐藏,反之则显示)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

方法二(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED); 
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘

自动弹出输入法

mCommentEdittext.setFocusable(true);
mCommentEdittext.setFocusableInTouchMode(true);
mCommentEdittext.requestFocus();
InputMethodManager inputManager = (InputMethodManager) mCommentEdittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(mCommentEdittext, 0);

调用隐藏系统默认的输入法

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken()
      , InputMethodManager.HIDE_NOT_ALWAYS);
WidgetSearchActivity是当前的Activity

获取输入法打开的状态

这个方法有点不太可靠android

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开

 计算屏幕的方式监听输入法是否打开

private void initGlobalLayoutListener(){
        mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
                    int mScreenHeight = 0;
                    int mKeyboardHeight = 0;
                    @Override
                    public void onGlobalLayout() {

                        Rect rect = new Rect();
                        // 测量当前窗口的显示区域
                        ((Activity)getContext()).getWindow().getDecorView()
                                .getWindowVisibleDisplayFrame(rect);
                        if(mScreenHeight <= 0){
                            mScreenHeight = ((WindowManager) getContext()
                                    .getSystemService(Context.WINDOW_SERVICE))
                                    .getDefaultDisplay().getHeight();
                        }
                        //计算出软键盘的高度
                        int keyboardHeight = mScreenHeight - rect.bottom;

                        //若是keyboardHeight大于屏幕的五分之一,
                        // 此时keyboardHeight有效,反之就是软键盘已经关闭了。
                        if (Math.abs(keyboardHeight) > mScreenHeight / 5) {
                            mKeyboardHeight = keyboardHeight;
                            L.e("已经触发键盘");
                        }else {
                            L.e("没有触发键盘");


                        }
                    }
                };
        mRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);//给xml里的根布局设置监听
    }

 记得移除监听ide

@Override
    public void onPause() {
        super.onPause();
        mRootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(mGlobalLayoutListener);
    }

进入activity就要显示输入法

在清单文件对应的activity配置中加入一句Android:windowSoftInputMode="stateVisible|adjustResize"布局

根据输入法位置改变,改变输入框位置

android:windowSoftInputMode的值adjustPan或者adjustResize便可,像这样:this

<activity
    android:name=".MainActivity" android:windowSoftInputMode="adjustPan" > ... </activity>

 

 

 

endspa

相关文章
相关标签/搜索