简体   繁体   English

Android - 屏幕键盘

[英]Android - OnScreen Keyboard

I know, it's asked many times, but for old Android versions and not all solutions working.我知道,它被问了很多次,但是对于旧的 Android 版本,并非所有解决方案都有效。

I need to programmatically control onscreen keyboard on Android 10 system:我需要在 Android 10 系统上以编程方式控制屏幕键盘:

  • Show onscreen keyboard when I want, not when want Android system (.).在需要时显示屏幕键盘,而不是在需要 Android 系统 (.) 时显示。
  • Hide onscreen keyboard when I want, not when want Android system (.).在需要时隐藏屏幕键盘,而不是在需要时隐藏 Android 系统 (.)。
  • Check whether onscreen keyboard is showed.检查是否显示屏幕键盘。
  • Prevent system from showing/hidding onscreen keyboard - only I will manipulate with it (.).防止系统显示/隐藏屏幕键盘 - 只有我会用它来操作 (.)。

Code can be in kotlin/java.代码可以在 kotlin/java 中。

Add these two properties to your parent layout (ex: Linear Layout, Relative Layout)将这两个属性添加到您的父布局(例如:线性布局、相对布局)

android:focusable="false"
android:focusableInTouchMode="false" 

in your activity:在您的活动中:

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Alternatively,或者,

  1. you could also declare in your manifest file's activity您还可以在清单文件的活动中声明

    <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Main" android:label="@string/app_name" android:windowSoftInputMode="stateHidden" >
  2. If you have already been using android:windowSoftInputMode for a value like adjustResize or adjustPan, you can combine two values:如果您已经将 android:windowSoftInputMode 用于诸如 adjustResize 或 adjustPan 之类的值,则可以组合两个值:

     <activity... android:windowSoftInputMode="stateHidden|adjustPan"... >

show keyboard programmatically:以编程方式显示键盘:

    public static boolean showKeyboard(View view) {
    if (view == null) {
        return false;
    }
    try {
        InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        view.requestFocus();
        return inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    } catch (Exception ignored) {
    }
    return false;
}

hide keyboard:隐藏键盘:

public static void hideKeyboard(View view) {
    if (view == null) {
        return;
    }
    try {
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (!imm.isActive()) {
            return;
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        view.clearFocus();
    } catch (Exception e) {
    }
}

on keyboard show listener:在键盘显示监听器上:

public static void OnkeyBoardShowListener(final Activity activity) {
final View activityRootView = activity.findViewById(android.R.id.content);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
 @Override
  public void onGlobalLayout() {
    Rect rect = new Rect();
    activityRootView.getWindowVisibleDisplayFrame(rect);
    int heightRoot = activityRootView.getRootView().getHeight();
    int heightDiff = heightRoot - rect.bottom;
    if (heightDiff > dpToPx(activity, 200)) {
          //Keyboard is open
    }else if (heightDiff < dpToPx(activity, 200)) {
         //Keyboard is close
    }
   }
 });
}

convert Dp to Px:将 Dp 转换为 Px:

private static float dpToPx(Context context, int dp) {
 DisplayMetrics metrics =  context.getResources().getDisplayMetrics();
 return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp, metrics);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM