├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ └── styles.xml
│ │ ├── drawable
│ │ │ ├── test.jpg
│ │ │ └── ic_launcher_background.xml
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ ├── layout
│ │ │ └── activity_main.xml
│ │ └── drawable-v24
│ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ └── com
│ │ │ └── zhangxq
│ │ │ └── softkeyboardtest
│ │ │ ├── MainActivity.java
│ │ │ └── HeightProvider.java
│ │ └── AndroidManifest.xml
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── README.md
├── .gitignore
└── gradle.properties
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SoftKeyboardTest
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/drawable/test.jpg
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # keyboardHeightProvider
2 | 设置activity的 android:windowSoftInputMode="adjustNothing"情况下,获取键盘高度
3 | 使用方法:复制HeightProvider类到项目中当成工具类使用即可
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxq/keyboardHeightProvider/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
11 | /gradle
12 | *.idea
13 | /gradlew
14 | /gradlew.bat
15 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | defaultConfig {
6 | applicationId "com.zhangxq.softkeyboardtest"
7 | minSdkVersion 19
8 | targetSdkVersion 26
9 | versionCode 1
10 | versionName "1.0"
11 | }
12 | buildTypes {
13 | release {
14 | minifyEnabled false
15 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
16 | }
17 | }
18 | }
19 |
20 | dependencies {
21 | implementation fileTree(dir: 'libs', include: ['*.jar'])
22 | implementation 'com.android.support:appcompat-v7:26.1.0'
23 | }
24 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zhangxq/softkeyboardtest/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.zhangxq.softkeyboardtest;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.widget.EditText;
6 |
7 | public class MainActivity extends AppCompatActivity {
8 | private EditText etBottom;
9 |
10 | @Override
11 | protected void onCreate(Bundle savedInstanceState) {
12 | super.onCreate(savedInstanceState);
13 | setContentView(R.layout.activity_main);
14 | etBottom = findViewById(R.id.etBottom);
15 |
16 | new HeightProvider(this).init().setHeightListener(new HeightProvider.HeightListener() {
17 | @Override
18 | public void onHeightChanged(int height) {
19 | etBottom.setTranslationY(-height);
20 | }
21 | });
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zhangxq/softkeyboardtest/HeightProvider.java:
--------------------------------------------------------------------------------
1 | package com.zhangxq.softkeyboardtest;
2 |
3 | import android.app.Activity;
4 | import android.graphics.Rect;
5 | import android.graphics.drawable.ColorDrawable;
6 | import android.view.Gravity;
7 | import android.view.View;
8 | import android.view.ViewTreeObserver.OnGlobalLayoutListener;
9 | import android.view.WindowManager.LayoutParams;
10 | import android.widget.PopupWindow;
11 |
12 | public class HeightProvider extends PopupWindow implements OnGlobalLayoutListener {
13 | private Activity mActivity;
14 | private View rootView;
15 | private HeightListener listener;
16 | private int heightMax; // 记录popup内容区的最大高度
17 |
18 | public HeightProvider(Activity activity) {
19 | super(activity);
20 | this.mActivity = activity;
21 |
22 | // 基础配置
23 | rootView = new View(activity);
24 | setContentView(rootView);
25 |
26 | // 监听全局Layout变化
27 | rootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
28 | setBackgroundDrawable(new ColorDrawable(0));
29 |
30 | // 设置宽度为0,高度为全屏
31 | setWidth(0);
32 | setHeight(LayoutParams.MATCH_PARENT);
33 |
34 | // 设置键盘弹出方式
35 | setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
36 | setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
37 | }
38 |
39 | public HeightProvider init() {
40 | if (!isShowing()) {
41 | final View view = mActivity.getWindow().getDecorView();
42 | // 延迟加载popupwindow,如果不加延迟就会报错
43 | view.post(new Runnable() {
44 | @Override
45 | public void run() {
46 | showAtLocation(view, Gravity.NO_GRAVITY, 0, 0);
47 | }
48 | });
49 | }
50 | return this;
51 | }
52 |
53 | public HeightProvider setHeightListener(HeightListener listener) {
54 | this.listener = listener;
55 | return this;
56 | }
57 |
58 | @Override
59 | public void onGlobalLayout() {
60 | Rect rect = new Rect();
61 | rootView.getWindowVisibleDisplayFrame(rect);
62 | if (rect.bottom > heightMax) {
63 | heightMax = rect.bottom;
64 | }
65 |
66 | // 两者的差值就是键盘的高度
67 | int keyboardHeight = heightMax - rect.bottom;
68 | if (listener != null) {
69 | listener.onHeightChanged(keyboardHeight);
70 | }
71 | }
72 |
73 | public interface HeightListener {
74 | void onHeightChanged(int height);
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------