├── README.md ├── SoftKeyBoardHelper.iml ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── liubin1 │ │ └── softkeyboardhelper │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── liubin1 │ │ │ └── softkeyboardhelper │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── liubin1 │ └── softkeyboardhelper │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradlew ├── gradlew.bat ├── local.properties └── settings.gradle /README.md: -------------------------------------------------------------------------------- 1 | # SoftKeyBoardHelper 2 | Android-app解决键盘遮挡注册或登录按钮 3 | 4 | ### 简单的几行代码解决键盘遮挡登录或注册按钮 5 | 6 | - 在项目开发中,这种情况是十分常见的,尤其是在登陆界面,登陆按钮经常被键盘挡住,导致用户输入完账号之后需要关闭键盘,然后再输入密码,然后再关掉键盘,点击登陆。十分繁琐,一旦用户输入错误,就要重复上述步骤。 7 | 8 | - 今天的这个小案例,就是一次性解决键盘遮挡问题,动态的计算,登陆按钮显示需要的高度,进而滚动布局,使之每次输入,键盘都无法遮挡输入框以及按钮。 9 | 10 | ### 来一张效果图: 11 | 12 | 13 | ![image](http://ww3.sinaimg.cn/mw690/005O1u7Gjw1f9sm4k991pg30eb0l77ch.gif ) 14 | 15 | 欢迎fork star 16 | -------------------------------------------------------------------------------- /SoftKeyBoardHelper.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "24.0.2" 6 | 7 | defaultConfig { 8 | applicationId "com.example.liubin1.softkeyboardhelper" 9 | minSdkVersion 16 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:25.0.0' 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\AndroidTools\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/liubin1/softkeyboardhelper/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.example.liubin1.softkeyboardhelper; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/liubin1/softkeyboardhelper/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.liubin1.softkeyboardhelper; 2 | 3 | import android.graphics.Rect; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.view.ViewTreeObserver; 8 | import android.widget.Button; 9 | import android.widget.EditText; 10 | import android.widget.RelativeLayout; 11 | 12 | public class MainActivity extends AppCompatActivity { 13 | 14 | private EditText name; 15 | private EditText pas; 16 | private EditText rpas; 17 | private Button res; 18 | private RelativeLayout main; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_main); 24 | initialize(); 25 | addLayoutListener(main,res); 26 | } 27 | 28 | private void initialize() { 29 | name = (EditText) findViewById(R.id.name); 30 | pas = (EditText) findViewById(R.id.pas); 31 | rpas = (EditText) findViewById(R.id.rpas); 32 | res = (Button) findViewById(R.id.res); 33 | main = (RelativeLayout) findViewById(R.id.main); 34 | } 35 | 36 | /** 37 | * 1、获取main在窗体的可视区域 38 | * 2、获取main在窗体的不可视区域高度 39 | * 3、判断不可视区域高度 40 | * 1、大于100:键盘显示 获取Scroll的窗体坐标 41 | * 算出main需要滚动的高度,使scroll显示。 42 | * 2、小于100:键盘隐藏 43 | * 44 | * @param main 根布局 45 | * @param scroll 需要显示的最下方View 46 | */ 47 | public void addLayoutListener(final View main, final View scroll) { 48 | main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 49 | @Override 50 | public void onGlobalLayout() { 51 | Rect rect = new Rect(); 52 | main.getWindowVisibleDisplayFrame(rect); 53 | int mainInvisibleHeight = main.getRootView().getHeight() - rect.bottom; 54 | if (mainInvisibleHeight > 100) { 55 | int[] location = new int[2]; 56 | scroll.getLocationInWindow(location); 57 | int srollHeight = (location[1] + scroll.getHeight()) - rect.bottom; 58 | main.scrollTo(0, srollHeight); 59 | } else { 60 | main.scrollTo(0, 0); 61 | } 62 | } 63 | }); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 16 | 24 | 32 |