Android-键盘遮挡登录或注册按钮
来源:互联网 发布:街机网络对战平台 编辑:程序博客网 时间:2024/06/09 09:10
简单的几行代码解决键盘遮挡登录或注册按钮
在项目开发中,这种情况是十分常见的,尤其是在登陆界面,登陆按钮经常被键盘挡住,导致用户输入完账号之后需要关闭键盘,然后再输入密码,然后再关掉键盘,点击登陆。十分繁琐,一旦用户输入错误,就要重复上述步骤。
今天的这个小案例,就是一次性解决键盘遮挡问题,动态的计算,登陆按钮显示需要的高度,进而滚动布局,使之每次输入,键盘都无法遮挡输入框以及按钮。
先来一张效果图:
进入代码实战:
- 极为简单的布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/main" tools:context="com.example.liubin1.softkeyboardhelper.MainActivity"> <EditText android:id="@+id/name" android:hint="请输入用户名:" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="50dp" /> <EditText android:id="@+id/pas" android:layout_below="@id/name" android:hint="请输入密 码:" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="50dp" /> <EditText android:id="@+id/rpas" android:layout_below="@id/pas" android:hint="请再输入密码:" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="50dp" /> <Button android:id="@+id/res" android:layout_below="@id/rpas" android:layout_centerHorizontal="true" android:text="注册" android:layout_width="180dp" android:layout_height="50dp" /></RelativeLayout>
简单的三个输入框和注册按钮。你只需要关注的是根布局main和注册按钮res
- 然后我们来看Activity
public class MainActivity extends AppCompatActivity { private EditText name; private EditText pas; private EditText rpas; private Button res; private RelativeLayout main; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initialize(); addLayoutListener(main,res); } private void initialize() { name = (EditText) findViewById(R.id.name); pas = (EditText) findViewById(R.id.pas); rpas = (EditText) findViewById(R.id.rpas); res = (Button) findViewById(R.id.res); main = (RelativeLayout) findViewById(R.id.main); } /** * 1、获取main在窗体的可视区域 * 2、获取main在窗体的不可视区域高度 * 3、判断不可视区域高度 * 1、大于100:键盘显示 获取Scroll的窗体坐标 * 算出main需要滚动的高度,使scroll显示。 * 2、小于100:键盘隐藏 * * @param main 根布局 * @param scroll 需要显示的最下方View */ public void addLayoutListener(final View main, final View scroll) { main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect rect = new Rect(); main.getWindowVisibleDisplayFrame(rect); int mainInvisibleHeight = main.getRootView().getHeight() - rect.bottom; if (mainInvisibleHeight > 100) { int[] location = new int[2]; scroll.getLocationInWindow(location); int srollHeight = (location[1] + scroll.getHeight()) - rect.bottom; main.scrollTo(0, srollHeight); } else { main.scrollTo(0, 0); } } }); }}
- OK ,里面的注释已经写的很清楚了。如果还有不明白的可以在下方给我留言。
重要的事情说三遍:
1、不需要在AndroidManifest.xml中配置键盘属性。
2、不需要在AndroidManifest.xml中配置键盘属性。
3、不需要在AndroidManifest.xml中配置键盘属性。
本文源代码:https://github.com/workuseliubin/SoftKeyBoardHelper 欢迎fork star
0 0
- Android-键盘遮挡登录或注册按钮
- 解决键盘遮挡注册或登录按钮
- android虚拟键盘弹出遮挡登陆按钮问题
- android带头像的登录界面键盘遮挡问题解决新思路
- 关于Android键盘遮挡
- Android 键盘遮挡问题
- android 键盘遮挡解决办法
- 弹出键盘遮挡按钮的处理方法
- 解决软键盘遮挡登录按钮
- Android小技巧 Part II——软键盘遮挡登录按钮
- Android如何避免输入法弹出时遮挡住按钮或输入框
- 键盘遮挡
- 键盘遮挡
- android输入框被键盘遮挡
- Android输入框被键盘遮挡
- android输入框被键盘遮挡
- android 输入键盘遮挡页面的问题
- 关于Android布局中键盘遮挡问题
- Linux应急常用命令
- xcode,在webView中引入本地html,image,js,css文件的方法
- Matlab与线性代数 -- 矩阵的秩
- 解决Mac OS X10.12下 Error watching file for changes: EMFILE及`brew link autoconf automake`错误
- Backbone入门指南(三):Events(事件管理)
- Android-键盘遮挡登录或注册按钮
- [BZOJ3450]Easy(期望dp)
- 关于go语言中的Slice切片问题
- 《深入理解jvm》读书笔记之——类加载机制(类的初始化)
- IE不支持js的checkbox遍历使用for in语法
- IOS 在自己的app内唤起地图APP
- 295. Find Median from Data Stream
- PAT(A).1002.A+B for Polynomials(25)
- python 爬虫爬取新东方考研真题