如题,我需要在一个应用中软键盘弹出和隐藏的时侯去做一些的操作,现在问题是如何可以捕获的到这两个事件呢??
我按网上提供最多的一种方法:【在androidManifest.xml里面定义:android:configChanges="orientation| keyboardHidden",然后在activity里面继承onConfigurationChanged的方法,处理上面的两个消息就OK了。 】去做了,但是当键盘弹出或者隐藏时,程序却压根没有去运行onConfigurationChanged,只有当横竖屏切换时才有运行。
跪求各位达人,这是什么原因啊?还有其他方法可以实现我的需求吗??
谢谢!!

解决方案 »

  1.   

    You can try to change the function in InputMethodService.java, the only position where get the soft keyboard information.keyboardHidden used to get the state of hardware keyboard.Or your can deal it in onWindowSizeChange().
      

  2.   

    谢谢2L的兄弟,按照你的方法,我在一个view里定义了一个接口,然后在其onlayout方法中去调用接口,因为在键盘弹出和隐藏时都运行了该view的onlayout方法。现在就是还有一个问题,我不知道有什么方法去获取当前的软键盘状态,网上一个方法:InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 然后if (imm.isActive()){...}这样做是不行的
      

  3.   

    1. You can't get the soft input panel state. Otherwise you've change the framework.
    2. I think it's not a good ideal to deal View.onLayout, it will be called frequently.
    3. I think deal the Activity.onWindowAttributeChange() is a better solution.
      

  4.   

    谢谢yyy025025025,我按照你的方法做了,是可以的,但是我如何去获取键盘当前的状态呢?我看文档,SDK只对外开放了InputMethodManager,它里面没有方法实时获取键盘状态,但是我看InputMethodService里有个isInputShow的方法可以达到要求,可惜的是好像无法获取到一个InputMethodService...
    请问有办法吗?谢谢
      

  5.   

    No function, otherwise you add API in inputmethodservice.
      

  6.   

    楼主用onWindowAttributeChange() 方法解决监听问题了吗?我的程序怎么调用不到这个方法呢?
      

  7.   

    // 在Manifest的配置Activity设置android:windowSoftInputMode="stateVisible|adjustResize"
    // 目前的我的View为整个窗口大小, 如果不是可能要调整
    // 希望对有些人有帮助或者提示左右,可能有更好的方法
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
    int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
    if(measuredHeight < getHeight() && input != null) { // 软键盘显示
    // @input 为我自定义组件, input.mRightBY 为该组件的下边界
    int distance = mInput.mRightBY - measuredHeight; // 计算滚动距离
    if(distance > 0) {
    scrollBy(0, distance); // 滚动
    }
    } else if(measuredHeight > getHeight()) { // 软键盘隐藏
    scrollTo(0, 0); // 恢复
    }
    setMeasuredDimension(measuredWidth, measuredHeight);
    }
      

  8.   

    向你那样做了 没有出结果 呀 @Override
    public void onWindowAttributesChanged(LayoutParams params) {
    // TODO Auto-generated method stub
    super.onWindowAttributesChanged(params);
    System.out
    .println("......................................................");
    }
    这个方法只有在初始化activity中调用一次以后就不再调用了 怎么个问题呀