zoukankan      html  css  js  c++  java
  • 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(), InputMethodManager.RESULT_UNCHANGED_SHOWN); //强制隐藏键盘

    自动弹出输入法

    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
    注意!以上的隐藏是调用activity的getCurrentFocus().getWindowToken().但是,这个调用在华为品牌的手机上会出现空指针报错.所以为了适配华为最安全的写法还是添加一个view,如下:
    mInputMethodManager.hideSoftInputFromWindow(mContentView.getWindowToken()
                            , InputMethodManager.HIDE_NOT_ALWAYS);//隐藏输入法

    获取输入法打开的状态

    这个方法有点不太可靠

    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里的根布局设置监听
        }

    正常activity调用

    private void initGlobalLayoutListener(){
            mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
                int mScreenHeight = 0;
                int mKeyboardHeight = 0;
                @Override
                public void onGlobalLayout() {
    
                    Rect rect = new Rect();
                    // 测量当前窗口的显示区域
                    DemoActivity.this.getWindow().getDecorView()
                            .getWindowVisibleDisplayFrame(rect);
                    if(mScreenHeight <= 0){
                        WindowManager windowManager = (WindowManager) DemoActivity.this.getSystemService(Context.WINDOW_SERVICE);
                        mScreenHeight = windowManager.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里的根布局设置监听
        }

    记得移除监听

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

    进入activity就要显示输入法

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

    不显示输入法

    android:windowSoftInputMode="stateHidden"

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

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

    <activity
        android:name=".MainActivity"
        android:windowSoftInputMode="adjustPan"  >
        ...
    </activity>
    • stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置
    • stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示
    • stateHidden:用户选择activity时,软键盘总是被隐藏
    • stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的
    • stateVisible:软键盘通常是可见的
    • stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态
    • adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示
    • adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间
    • adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分

    输入法弹出后,不压缩布局,直接覆盖布局

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

    在Fragment里设置输入法

    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    end

  • 相关阅读:
    linux 命令
    http 协议
    关于 yaf路由
    yaf学习 从头开始
    插件概念
    框架与设计模式的区别
    一个程序员的创业(爱情)故事
    话说那年微信接口平台创业旧事
    推荐sinaapp谷歌搜索引擎,firefox自定义搜索引擎
    新的开源java反汇编程序Procyon
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/11076295.html
Copyright © 2011-2022 走看看