Android优雅的方式解决软键盘遮挡按钮问题
前言
比如在进行登录的操作中,用户输入完密码之后,肯定是想直接点击登录按钮的。返回键隐藏软键盘这样的体验肯定很糟糕,程序员,遇到问题解决问题。
实现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 Studio安装完后activity_main.xml前几行报错的解决建议
这篇文章主要介绍了关于Android Studio安装完后activity_main.xml前几行报错的解决建议,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-03-03详解AndroidStudio中代码重构菜单Refactor功能
这篇文章主要介绍了AndroidStudio中代码重构菜单Refactor功能详解,本文通过代码演示,功能截图来详细说明as为大名重构提供的各项功能,需要的朋友可以参考下2019-11-11Android开发中实现IOS风格底部选择器(支持时间 日期 自定义)
这篇文章主要介绍了Android开发中实现IOS风格底部选择器(支持时间 日期 自定义)的相关资料,需要的朋友可以参考下2016-11-11解决android studio卡顿,提升studio运行速度的方法
这篇文章主要介绍了解决android studio卡顿,提升studio运行速度的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-03-03Android性能优化之RecyclerView分页加载组件功能详解
这篇文章主要为大家介绍了Android性能优化之RecyclerView分页加载组件功能详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-09-09LayoutAnimation给ListView中的item设置动态出场效果(实例)
下面小编就为大家带来一篇LayoutAnimation给ListView中的item设置动态出场效果(实例)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-10-10Android布局控件DrawerLayout实现完美侧滑效果
这篇文章主要为大家详细介绍了Android布局控件DrawerLayout实现完美侧滑效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-08-08
最新评论