1.通过代码方式隐藏软键盘.

 ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(getCurrentFocus()
        .getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);

2.通过代码方式开启软件盘.

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);    

if (imm.isActive()) {  
     imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);   
}  

3.如何禁止软键盘弹出.

1).在XML文件里  添加一个TextView
    譬如:                              android:id="@+id/tv"
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:focusableInTouchMode="true"/>
            注:android:focusableInTouchMode="true"一定要有


2). TextView tv = (TextView) findViewById(R.id.textview);

               tv.requestFocus();

4.如何获取软键盘高度.

1).全局变量
            private RelativeLayout rl;//父布局id
            private int statusBarHeight;

        2).
           final Context context = getApplicationContext();
           rl.getViewTreeObserver(). addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                 public void onGlobalLayout() {
                      Rect r = new Rect();
                      // r will be populated with the coordinates of your view that area still visible.
                      rl.getWindowVisibleDisplayFrame(r);
                      int screenHeight = rl.getRootView().getHeight();
                      int heightDiff = screenHeight - (r.bottom - r.top);
                      if (heightDiff > 100)
                           statusBarHeight = 0;
                      try {
                           Class<?> c = Class.forName("com.android.internal.R$dimen");
                           Object obj = c.newInstance();
                           Field field = c.getField("status_bar_height");
                           int x = Integer.parseInt(field.get(obj).toString());
                           statusBarHeight = context.getResources().getDimensionPixelSize(x);
                      } catch (Exception e) {
                           e.printStackTrace();
                      }
                     int realKeyboardHeight = heightDiff - statusBarHeight;


                     Log.i("print","软键盘高度: " + realKeyboardHeight);
                 }
             });


5.如何监听软键盘的弹出与隐藏状态.

1).最外层父布局.
     rl = (RelativeLayout) findViewById(R.id.rl);


2).下面这些代码不要在onCreate()方法里:

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

            rl.getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {

                        int heightDiff = rl.getRootView().getHeight() - rl.getHeight();
                        if (heightDiff > 200) {
                            
                           弹出的时候处理的逻辑
                            
                        } else {
                           
                            隐藏的时候处理的逻辑
                        }
                    }
                });

更多相关文章

  1. Android 检测键盘是否显示及隐藏键盘
  2. Android平台mass storage相关代码
  3. 使用代码为textview设置drawableLeft
  4. [置顶] Android 2.3.5源代码 更新至android 6.0,可以下载,度娘网盘
  5. 关于LinearLayout布局中,子控件平分宽度
  6. Android应用程序键盘(Keyboard)消息处理机制分析(12)
  7. cocos2dx 调用java层代码

随机推荐

  1. 20210111 装饰器之案例剖析
  2. 20210108 装饰器详解
  3. Vue自学之路3-vue模版初探
  4. 20210104 递归
  5. 210329 CSS 媒体查询 grid布局
  6. 大数据技术完美地解决了海量数据问题,可为
  7. DolphinDB的权限管理和安全
  8. 如何开始一个数据科学项目?
  9. Java并发编程学习3-可见性和对象发布
  10. 如何预测您的用户即将流失?