Android优雅的方式解决软键盘遮挡按钮问题
发布时间 - 2026-01-10 22:44:05 点击率:次前言

比如在进行登录的操作中,用户输入完密码之后,肯定是想直接点击登录按钮的。返回键隐藏软键盘这样的体验肯定很糟糕,程序员,遇到问题解决问题。
实现1
xml
<ScrollView android:id="@+id/scrollview" android:layout_width="match_parent" android:layout_height="wrap_content" android:fadingEdge="none" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:src="@mipmap/ic_loginhead"/> <EditText android:id="@+id/et_usernamelogin_username" style="@style/customEditText" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="10dp" android:background="@null" android:hint="请输入已验证手机" android:inputType="number" android:lines="1" android:maxLength="11"/> <ImageView android:layout_width="match_parent" android:layout_height="2px" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:background="@color/pating_line"/> <EditText android:id="@+id/et_usernamelogin_password" style="@style/customEditText" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="20dp" android:background="@null" android:digits="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_?" android:hint="请输入密码" android:inputType="textPassword"/> <ImageView android:layout_width="match_parent" android:layout_height="2px" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:background="@color/pating_line"/> <Button android:id="@+id/btn_usernamelogin_dologin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_marginTop="30dp" android:background="@drawable/btn_selecter" android:enabled="false" android:text="登录" android:textColor="@color/white" /> </LinearLayout> </ScrollView>
java
mScrollView=(ScrollView)view.findViewById(R.id.scrollview);
usernamelogin_username.setOnTouchListener(newView.OnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
changeScrollView();
returnfalse;
}
});
usernamelogin_password.setOnTouchListener(newView.OnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
changeScrollView();
returnfalse;
}
});
/**
*使ScrollView指向底部
*/
privatevoidchangeScrollView(){
newHandler().postDelayed(newRunnable(){
@Override
publicvoidrun(){
mScrollView.scrollTo(0,mScrollView.getHeight());
}
},300);
}
实现2
xml同上
anim下新建gone.xml
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="500" android:repeatCount="0"/>
visiable.xml
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="500" android:repeatCount="0"/>
或者直接在代码中
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.support.v7.app.AppCompatActivity;
importandroid.view.KeyEvent;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.animation.Animation;
importandroid.view.animation.AnimationSet;
importandroid.view.animation.ScaleAnimation;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.ImageView;
publicclassMainActivityextendsAppCompatActivity{
privateImageViewmHead;//头部ImageView
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHead=(ImageView)findViewById(R.id.iv_head);
finalButtonbtn=(Button)findViewById(R.id.btn_usernamelogin_dologin);
finalEditTextet_pass=(EditText)findViewById(R.id.et_usernamelogin_password);
finalEditTextet_name=(EditText)findViewById(R.id.et_usernamelogin_username);
/**
*当输入被点击
*/
et_name.setOnTouchListener(newView.OnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
start();
returnfalse;
}
});
btn.setEnabled(false);
btn.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
}
});
}
privatevoidstart(){
AnimationSetanimationSet=newAnimationSet(true);
ScaleAnimationscaleAnimation=newScaleAnimation(
1,0.1f,1,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setDuration(500);
animationSet.addAnimation(scaleAnimation);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setRepeatCount(0);//设置重复次数
mHead.startAnimation(scaleAnimation);
newHandler().postDelayed(newRunnable(){
@Override
publicvoidrun(){
mHead.setVisibility(View.GONE);
}
},500);
}
/**
*菜单、返回键响应
*/
@Override
publicbooleanonKeyDown(intkeyCode,KeyEventevent){
//TODOAuto-generatedmethodstub
if(keyCode==KeyEvent.KEYCODE_BACK){
if(mHead.getVisibility()==View.GONE){
AnimationSetanimationSet=newAnimationSet(true);
ScaleAnimationscaleAnimation=newScaleAnimation(
0.1f,1f,0.1f,1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setDuration(500);
animationSet.addAnimation(scaleAnimation);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
mHead.startAnimation(scaleAnimation);
mHead.setVisibility(View.VISIBLE);
}else{
finish();
}
}
returnfalse;
}
}
效果呢:
以上所述是小编给大家介绍的Android优雅的方式解决软键盘遮挡按钮问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# android
# 软键盘遮挡按钮
# Android开发之WebView输入框提示解决办法
# Android开发软键盘遮挡登陆按钮的完美解决方案
# Android软键盘遮挡的四种完美解决方案
# Android WebView软键盘遮挡输入框方案详解
# 请输入
# 小编
# 在此
# 给大家
# 解决问题
# 如在
# 所述
# 给我留言
# 感谢大家
# 直接点击
# 很糟糕
# 疑问请
# 有任何
# digits
# et_usernamelogin_password
# drawable
# btn_usernamelogin_dologin
# textPassword
# Button
# pating_line
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
微信小程序 scroll-view组件实现列表页实例代码
如何用PHP快速搭建CMS系统?
如何用已有域名快速搭建网站?
EditPlus中的正则表达式 实战(4)
Laravel如何处理异常和错误?(Handler示例)
如何撰写建站申请书?关键要点有哪些?
nodejs redis 发布订阅机制封装实现方法及实例代码
如何自定义safari浏览器工具栏?个性化设置safari浏览器界面教程【技巧】
phpredis提高消息队列的实时性方法(推荐)
Laravel Vite是做什么的_Laravel前端资源打包工具Vite配置与使用
图册素材网站设计制作软件,图册的导出方式有几种?
如何获取PHP WAP自助建站系统源码?
Laravel怎么判断请求类型_Laravel Request isMethod用法
Laravel Asset编译怎么配置_Laravel Vite前端构建工具使用
高端建站三要素:定制模板、企业官网与响应式设计优化
家族网站制作贴纸教程视频,用豆子做粘帖画怎么制作?
Gemini手机端怎么发图片_Gemini手机端发图方法【步骤】
Laravel Blade模板引擎语法_Laravel Blade布局继承用法
googleplay官方入口在哪里_Google Play官方商店快速入口指南
javascript如何操作浏览器历史记录_怎样实现无刷新导航
如何在阿里云高效完成企业建站全流程?
零基础网站服务器架设实战:轻量应用与域名解析配置指南
制作企业网站建设方案,怎样建设一个公司网站?
Android仿QQ列表左滑删除操作
如何在万网开始建站?分步指南解析
Laravel如何实现事件和监听器?(Event & Listener实战)
Laravel如何实现一对一模型关联?(Eloquent示例)
香港服务器WordPress建站指南:SEO优化与高效部署策略
弹幕视频网站制作教程下载,弹幕视频网站是什么意思?
如何在阿里云虚拟主机上快速搭建个人网站?
制作旅游网站html,怎样注册旅游网站?
Laravel如何配置和使用缓存?(Redis代码示例)
如何用VPS主机快速搭建个人网站?
矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?
如何生成腾讯云建站专用兑换码?
Python高阶函数应用_函数作为参数说明【指导】
米侠浏览器网页图片不显示怎么办 米侠图片加载修复
如何在橙子建站上传落地页?操作指南详解
如何快速搭建高效WAP手机网站吸引移动用户?
bing浏览器学术搜索入口_bing学术文献检索地址
logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?
Laravel模型事件有哪些_Laravel Model Event生命周期详解
Laravel如何从数据库删除数据_Laravel destroy和delete方法区别
零服务器AI建站解决方案:快速部署与云端平台低成本实践
Laravel如何集成微信支付SDK_Laravel使用yansongda-pay实现扫码支付【实战】
Java解压缩zip - 解压缩多个文件或文件夹实例
Laravel怎么集成Vue.js_Laravel Mix配置Vue开发环境
详解CentOS6.5 安装 MySQL5.1.71的方法
Laravel怎么实现支付功能_Laravel集成支付宝微信支付
Win11怎么修改DNS服务器 Win11设置DNS加速网络【指南】

