Android中软键盘使用总结(一)

xiaoxiao2021-02-27  225

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     譬如:<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 {                                                         隐藏的时候处理的逻辑                         }                     }                 });

转载请注明原文地址: https://www.6miu.com/read-9230.html

最新回复(0)