欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android优雅的方式解决软键盘遮挡按钮问题

程序员文章站 2023-12-21 20:34:22
前言 比如在进行登录的操作中,用户输入完密码之后,肯定是想直接点击登录按钮的。返回键隐藏软键盘这样的体验肯定很糟糕,程序员,遇到问题解决问题。 实现1 xml...

前言

比如在进行登录的操作中,用户输入完密码之后,肯定是想直接点击登录按钮的。返回键隐藏软键盘这样的体验肯定很糟糕,程序员,遇到问题解决问题。

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);
 }

Android优雅的方式解决软键盘遮挡按钮问题

实现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优雅的方式解决软键盘遮挡按钮问题,希望对大家有所帮助

上一篇:

下一篇: