对于Android键盘事件Google并没有提供一个好的接口去监听它,有时候就为项目需要就必须要自己去想办法去监听,由于我最近也要实现登陆与注册的功能,我的想法很简单实现起来也比较容易,主要的原理是在将Activity的配置android:windowSoftInputMode设置成adjust_resize当然设成其它我觉得也是可以的只是这还没有测试,这样在键盘弹出时Android会将而已进行调整,直接上代码呢。

设置inputMode

 1 <activity 2             android:name=".MainActivity" 3             android:label="@string/app_name" 4             android:windowSoftInputMode="adjustResize" 5             > 6             <intent-filter> 7                 <action android:name="android.intent.action.MAIN" /> 8  9                 <category android:name="android.intent.category.LAUNCHER" />10             </intent-filter>11         </activity>

设置监听而已变化

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //android.R.id.content 这是包含布局的父控件ID        View contentView = findViewById(android.R.id.content);
     //设置而已监听器 contentView.addOnLayoutChangeListener(this); }

根据值的变化判断键盘是否显示。

       
  @Override    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {        if(bottom < oldBottom){            onKeyboardState(true);        }else if(bottom > oldBottom){            onKeyboardState(false);        }    }
   

这样基本上就可以实现键盘的监听呢,我这样写基本上可以满足大部分需求。

更多相关文章

  1. android软键盘把页面顶上去
  2. Android:解析 SurfaceView & TextureView
  3. Android中自定义组合控件
  4. Android单选和多选按钮的使用
  5. Android(安卓)Framework 之HelloWorld(一)
  6. Android(安卓)中代码去模拟人的操作
  7. android GridView学习笔记
  8. android样式
  9. Android性能优化之一:ViewStub

随机推荐

  1. android scrollview中嵌套expandablelist
  2. android 用沉浸式隐藏status bar
  3. Frist Android
  4. Android Bitmap换背景颜色
  5. Android 软键盘相关辅助类KeyBoardUtils
  6. "Android中的动画"-Android面试必问"精华
  7. Android异步线程OkHttp Post请求Json数据
  8. Porting Android
  9. Android ViewGroup事件分发机制
  10. android activity 属性解释