登录界面经常会出现输入框被键盘遮挡的情况,这里处理的方式是当软件盘出现的时候在界面的最底部空出键盘的高度。需要代码和xml布局一起修改来实现。

1、xml布局通过ScrollView包裹住内容,内容使用LinearLayout包裹。

<?xml version="1.0" encoding="utf-8"?>
<NonFocusingScrollViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/sv_content"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"tools:context="cn.genomics.capture.view.LoginActivity"><LinearLayoutandroid:id="@+id/view_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="vertical"><ImageViewandroid:layout_width="@dimen/space_80dp"android:layout_height="@dimen/space_80dp"android:layout_marginTop="@dimen/space_50dp"android:background="@drawable/bg_login"android:padding="@dimen/space_15dp"android:src="@mipmap/ic_login_logo"android:text="登录"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="@dimen/space_15dp"android:text="MyBGI"android:textColor="@color/colorPrimary"android:textSize="@dimen/text_size_16sp"android:textStyle="bold"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="51dp"android:layout_marginTop="@dimen/space_30dp"android:paddingLeft="33.5dp"android:paddingRight="33.5dp"><cn.genomics.capture.view.widget.ClearEditTextandroid:id="@+id/phone_number"android:layout_width="match_parent"android:layout_height="50dp"android:background="@null"android:hint="@string/prompt_user"android:imeActionId="@+id/login"android:imeOptions="actionUnspecified"android:inputType="text"android:singleLine="true"android:textColor="@color/colorPrimary"android:textColorHint="@color/text_color_item_hint"android:textSize="@dimen/text_size_14sp"/><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:layout_alignParentBottom="true"android:background="@color/text_color_item_hint"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="51dp"android:layout_marginTop="@dimen/space_10dp"android:paddingLeft="33.5dp"android:paddingRight="33.5dp"><cn.genomics.capture.view.widget.ClearEditTextandroid:id="@+id/password"android:layout_width="match_parent"android:layout_height="50dp"android:background="@null"android:hint="@string/prompt_password"android:inputType="textPassword"android:singleLine="true"android:textColor="@color/colorPrimary"android:textColorHint="@color/text_color_item_hint"android:textSize="@dimen/text_size_14sp"/><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:layout_alignParentBottom="true"android:background="@color/text_color_item_hint"/></RelativeLayout><Buttonandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_height="@dimen/space_120dp"android:layout_marginTop="@dimen/space_40dp"android:background="@drawable/btn_login"android:gravity="top|center_horizontal"android:paddingTop="38dp"android:text="@string/action_sign_in"android:textColor="@color/white"/></LinearLayout>
</NonFocusingScrollView>

其中 NonFocusingScrollView 是自定义的ScrollView用来让ScrollView不自动切换子view的焦点

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ScrollView;/**
* 自定义ScrollView,不让ScrollView自动切换子view的焦点
* @author chentuanhui
* created at 2017/4/28 11:18
**/
public class NonFocusingScrollView extends ScrollView {public NonFocusingScrollView(Context context) {super(context);}public NonFocusingScrollView(Context context, AttributeSet attrs) {super(context, attrs);}public NonFocusingScrollView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}/*** TODO 不让ScrollView自动切换子View的焦点* @param direction* @param previouslyFocusedRect* @return*/@Overrideprotected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {return true;}}

activity对应的java代码

public class LoginActivity extends AppCompatActivity implements OnClickListener {private EditText mUserNameEt;private EditText mPasswordEt;private Button mLoginBtn;private LoginPresenter mPresenter;private long mTime;private Handler mHandler = new Handler();private ScrollView mContainerSv;private View mContentView;private View mContainer;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);mContainer = LayoutInflater.from(this).inflate(R.layout.activity_login, null);setContentView(mContainer);initView();setListener();}protected void initView() {mUserNameEt = (EditText) findViewById(R.id.phone_number);mPasswordEt = (EditText) findViewById(R.id.password);mLoginBtn = (Button) findViewById(R.id.btn_login);mContainerSv = (ScrollView) findViewById(R.id.sv_content);mContentView = findViewById(R.id.view_content);}protected void setListener() {mLoginBtn.setOnClickListener(this);mContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {Rect r = new Rect();mContainer.getWindowVisibleDisplayFrame(r);int screenHeight = mContainer.getRootView().getHeight();// r.bottom is the position above soft keypad or device button.// if keypad is shown, the r.bottom is smaller than that before.int keypadHeight = screenHeight - r.bottom;if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.// 当键盘显示的时候走这里,最底部空出键盘占用的高度mContentView.setPadding(0, 0, 0, keypadHeight);//延迟滚动到底部,为了防止焦点出现跳动new Handler().postDelayed(new Runnable() {@Overridepublic void run() {//将ScrollView滚动到最底部mContainerSv.fullScroll(View.FOCUS_DOWN);}}, 100);} else {// 当键盘隐藏的时候走这里,还原默认的显示mContentView.setPadding(0, 0, 0, 0);}}});}
}

到这里完美解决键盘遮挡问题,代码也特别的简单。

android,键盘遮挡了输入框,完美解决android键盘遮挡问题相关推荐

  1. android打开app白色页面,完美解决Android App启动页有白屏闪过的问题

    应用启动的时候有短暂的白屏,如图: 可以通过设置theme的方式来解决 @color/colorPrimary @color/colorPrimaryDark @color/colorAccent t ...

  2. rn android软键盘遮挡布局处理,完美解决RN 键盘遮挡问题

    当输入框在底部的时候, 唤起键盘会造成遮挡问题,解决方法如下: class ForumDetail extends React.Component { render(){ let behavior = ...

  3. (转)完美解决 Android WebView 文本框获取焦点后自动放大有关问题

    完美解决 Android WebView 文本框获取焦点后自动放大问题 前几天在写一个项目时,要求在项目中嵌入一个WebView 本来很快就完成了,测试也没有问题.但发给新加坡时,他们测试都会出现文本 ...

  4. Android输入法遮挡了输入框,使用android:fitsSystemWindows=“true“后界面顶部出现白条解决方案

    Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条解决方案 参考文章: (1)Android输入法遮挡了输入框 ...

  5. android studio crashlytics,完美解决Android Studio集成crashlytics后无法编译的问题

    问题描述: 在用fabric集成后编译出现如下错误, Error:Cause: hostname in certificate didn't match: != OR OR build.gradle部 ...

  6. android studio try again,完美解决Android Studio在gradle上的各种问题

    原标题:完美解决Android Studio在gradle上的各种问题 题记: 看到很多人都来读这篇文章,说明很多人都有遇到这个问题,文章质量不是很高,感觉我自己都有些看不懂了,因此来更新一下,希望可 ...

  7. android 通知过滤,冰箱 IceBox 开发者新作,完美解决 Android 通知过滤问题的 APP

    原标题:冰箱 IceBox 开发者新作,完美解决 Android 通知过滤问题的 APP 在用 Android 手机时,我们会有一些应用没办法不用,但是又想让它没有不用这时候就像卸载那样安安静静地待在 ...

  8. 5种方法完美解决android软键盘挡住输入框方法详解

    版权声明:本文为CSDN博主「潇潇凤儿」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明. 原文链接:https://blog.csdn.net/smileiam/ ...

  9. android导航栏自动弹出,解决android 显示内容被底部导航栏遮挡的问题

    描述: 由于产品需求,要求含有EditText的界面全屏显示,最好的解决方式是使用AndroidBug5497Workaround.assistActivity(this) 的方式来解决,但是华为和魅 ...

最新文章

  1. linux 根目录下的子目录的意义
  2. python调换字符串顺序_python实现指定字符串交换
  3. 【搜索引擎Jediael开发4】V0.01完整代码
  4. kali怎么成为管理员_网站死链是什么、是怎么引起的以及死链对SEO优化的影响?...
  5. 裁判打分_内在的裁判偏见
  6. kex_exchange_identification: Connection closed by remote host Connection closed by 140.82.121.3 port
  7. 安全认证传输服务器介绍
  8. jsp if else c标签 总结
  9. UNIX系统操作入门
  10. java 19 -2 异常以及tr...catch()的方法处理异常
  11. 20. jQuery 遍历 - 祖先
  12. Python学习之路_day_11(装饰器)
  13. VRay材质练习(一):水、玻璃、牛奶
  14. burst tx 功能 开启_华硕路由器无线网络,专业设置各功能科普
  15. 云杰恒指:7.29恒指期货实盘指导交易复盘
  16. 超实用的Mac风扇控制系统:Macs Fan Control Pro mac中文版
  17. 大数据技术在我们日常生活中的应用
  18. 如何禁止电脑随便修改IP?
  19. 华为云Stack深度驱动金融行业智能化升级
  20. 极验验证码破解之selenium

热门文章

  1. 如何修复win7蓝牙服务器,恢复Windows7系统超便捷蓝牙连接
  2. win10企业版LTSC-1809无法访问共享文件
  3. 【小程序】之net::ERR_NAME_NOT_RESOLVED的问题
  4. IBM__P系列 小型机 故障定位 故障排除
  5. 如何设计一个单点登录系统
  6. 数字转换为十六进制数
  7. [SSL: CERTIFICATE_VERIFY_FAILED]
  8. gitlab 安装以及卸载
  9. 王权游戏中“帽子王”成就的获取
  10. MIPI屏和LVDS屏有什么区别