
在最近的两个项目中,项目需求要求我们实现 /*登陆页面的内容能够随着键盘的d出而被顶上去,避免键盘遮挡住登陆按钮*/ 这样的效果,宝宝心里苦呀,本来半天搞定的事还非得折腾一下,好吧我妥协,毕竟我还是一只非常注重用户体验的猿。
那就做吧,初步定下的方案是输入框和登陆按钮大小不变,在键盘d出的时候让logo的大小和位置进行改变,从而给键盘腾出位置,当然在键盘收起的时候还要给它还原一下,就像什么都没发生一样,嗯对,就是这样,说了这么多,放张图先感受一下效果吧:
接下来上正餐,布局上比较简单,注意给图片外边套上一个合身的linearlayout就好,因为待会要靠它改变logo的位置,布局代码如下:
<?xml version="1.0" enCoding="utf-8"?> <linearLayout 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:background="@color/white" tools:context=".login.LoginActivity" androID:orIEntation="vertical" androID:ID="@+ID/ll_login_root"> <linearLayout androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margintop="90dp" androID:ID="@+ID/ll_login_logobg" androID:layout_marginBottom="50dp"> <ImageVIEw androID:layout_wIDth="160dp" androID:layout_height="160dp" androID:ID="@+ID/iv_login_logo" androID:background="@mipmap/login_logo" androID:layout_gravity="center_horizontal" /> </linearLayout> <linearLayout androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_marginleft="50dp" androID:layout_marginRight="50dp"> <linearLayout androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="50dp"> <ImageVIEw androID:layout_wIDth="45dp" androID:layout_height="45dp" androID:background="@mipmap/login_phone" androID:ID="@+ID/imageVIEw2" androID:layout_gravity="bottom" /> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:inputType="phone" androID:ems="10" androID:ID="@+ID/et_login_phone" androID:layout_gravity="center" androID:hint="请输入手机号" androID:background="@null" androID:maxLength="11"/> </linearLayout> <linearLayout androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="1dp" androID:background="@color/text_gray"></linearLayout> <linearLayout androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="50dp" > <ImageVIEw androID:layout_wIDth="45dp" androID:layout_height="45dp" androID:background="@mipmap/login_password" androID:ID="@+ID/imageVIEw3" androID:layout_gravity="bottom" /> <EditText androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:inputType="phone" androID:ems="10" androID:ID="@+ID/et_login_code" androID:layout_gravity="center" androID:layout_weight="1" androID:hint="请输入验证码" androID:background="@null" androID:maxLength="6"/> <button androID:layout_wIDth="90dp" androID:layout_height="30dp" androID:text="获取验证码" androID:textcolor="@color/white" androID:ID="@+ID/bt_login_getcode" androID:background="@mipmap/login_button_blue" androID:layout_gravity="center_vertical" androID:textSize="14dp" /> </linearLayout> <linearLayout androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="1dp" androID:background="@color/text_gray" androID:layout_marginBottom="10dp" /> <linearLayout androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="20dp" androID:ID="@+ID/ll_login_warning" androID:visibility="gone"> <ImageVIEw androID:layout_wIDth="25dp" androID:layout_height="25dp" androID:background="@mipmap/login_warning" androID:ID="@+ID/imageVIEw" androID:layout_gravity="center_vertical" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textAppearance="?androID:attr/textAppearanceMedium" androID:text="请输入验证码" androID:ID="@+ID/tv_login_wraning" androID:layout_gravity="center_vertical" androID:textcolor="@color/text_red" androID:textSize="13dp" /> </linearLayout> <button androID:layout_wIDth="match_parent" androID:layout_height="40dp" androID:textcolor="@color/white" androID:ID="@+ID/bt_login_submit" androID:background="@mipmap/login_button_gray" androID:text="登 录" androID:textSize="18dp" androID:layout_margintop="10dp" /> </linearLayout> </linearLayout>
主代码如下,我会把注释添加到代码中,因为是整个模块的代码所以也会有一些其他功能在里边:
package com.millionIDeas.cm.login; import androID.app.ProgressDialog; import androID.content.Intent; import androID.os.Bundle; import androID.os.Handler; import androID.text.Editable; import androID.text.TextWatcher; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.Widget.button; import androID.Widget.EditText; import androID.Widget.ImageVIEw; import androID.Widget.linearLayout; import androID.Widget.TextVIEw; import com.millionIDeas.cm.R; import com.millionIDeas.cm.home.HomeActivity; import com.millionIDeas.cm.main.BaseActivity; import com.millionIDeas.cm.tools.TimeCountUtils; import org.xutils.vIEw.annotation.ContentVIEw; import org.xutils.vIEw.annotation.Event; import org.xutils.vIEw.annotation.VIEwInject; import java.util.regex.Matcher; import java.util.regex.Pattern; @ContentVIEw(R.layout.activity_login) public class LoginActivity extends BaseActivity implements VIEw.OnLayoutchangelistener{ //用xUtils进行控件绑定 @VIEwInject(R.ID.iv_login_logo) ImageVIEw iv_login_logo; @VIEwInject(R.ID.ll_login_logobg) linearLayout ll_login_logobg; @VIEwInject(R.ID.et_login_phone) EditText et_login_phone; @VIEwInject(R.ID.et_login_code) EditText et_login_code; @VIEwInject(R.ID.ll_login_warning) linearLayout ll_login_warning; @VIEwInject(R.ID.tv_login_wraning) TextVIEw tv_login_wraning; @VIEwInject(R.ID.bt_login_getcode) button bt_login_getcode; @VIEwInject(R.ID.bt_login_submit) button bt_login_submit; @VIEwInject(R.ID.ll_login_root) linearLayout activityRootVIEw;//需要 *** 作的布局 private TimeCountUtils timeCountUtils; private Matcher phone_num; private Pattern phonenumber; private ProgressDialog progressDialog; private Handler handler; private int screenHeight = 0;//屏幕高度 private int keyHeight = 0; //软件盘d起后所占高度 @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); screenHeight = this.getwindowManager().getDefaultdisplay().getHeight(); //获取屏幕高度 keyHeight = screenHeight / 3;//d起高度为屏幕高度的1/3 timeCountUtils = new TimeCountUtils(LoginActivity.this,60000,1000,bt_login_getcode);//时间工具类用以实现倒计时 progressDialog=new ProgressDialog(this);//对话框 handler=new Handler(); bt_login_submit.setClickable(false); et_login_phone.addTextChangedListener(new TextWatcher() {//为edittext添加文本改变监听,根据是否有文本输入更改确认按钮的背景颜色 @OverrIDe public voID beforeTextChanged(CharSequence charSequence,int i,int i1,int i2) { } @OverrIDe public voID onTextChanged(CharSequence charSequence,int i2) { } @OverrIDe public voID afterTextChanged(Editable editable) { if (!et_login_phone.getText().toString().equals("")){ bt_login_submit.setClickable(true); bt_login_submit.setBackgroundResource(R.drawable.login_button); }else { bt_login_submit.setClickable(false); bt_login_submit.setBackgroundResource(R.mipmap.login_button_gray); } } }); } //xUtils的事件处理 @Event(value = {R.ID.bt_login_submit,R.ID.bt_login_getcode},type = VIEw.OnClickListener.class) private voID onClick(VIEw vIEw) { switch (vIEw.getID()) { case R.ID.bt_login_submit://确认按钮事件 if (!CheckPhone(et_login_phone).matches()) {//判断手机号格式 ll_login_warning.setVisibility(VIEw.VISIBLE); tv_login_wraning.setText("手机号码格式不正确"); } else if (!et_login_code.getText().toString().equals("000")) {//验证码判断,为方便测试设置了默认值 ll_login_warning.setVisibility(VIEw.VISIBLE); tv_login_wraning.setText("验证码不正确"); } else {//条件全部满足,开始登陆 ll_login_warning.setVisibility(VIEw.GONE); progressDialog.setMessage("正在登录…"); progressDialog.show();//d出加载对话框 handler.postDelayed(new Runnable() {//设置一个1s的延时 *** 作模拟登陆的过程 @OverrIDe public voID run() {//登陆成功关掉对话框,跳转页面,关掉本页 progressDialog.dismiss();//不能用hIDe Intent intent=new Intent(LoginActivity.this,HomeActivity.class); startActivity(intent); LoginActivity.this.finish(); } },1000); } break; case R.ID.bt_login_getcode: if (CheckPhone(et_login_phone).matches()) {//手机号正确则获取验证码,开启倒计时 ll_login_warning.setVisibility(VIEw.GONE); bt_login_getcode.setBackgroundResource(R.mipmap.login_button_gray); timeCountUtils.start(); } else { ll_login_warning.setVisibility(VIEw.VISIBLE); tv_login_wraning.setText("手机号码格式不正确"); } break; } } public Matcher CheckPhone(EditText editText) {//判断手机号格式 phonenumber = Pattern .compile("^[1][3-8][0-9]{9}$"); phone_num = phonenumber.matcher(editText.getText() .toString()); return phone_num; } @OverrIDe protected voID onResume() { super.onResume(); activityRootVIEw.addOnLayoutchangelistener(this);//给需要 *** 作的布局设置监听 } @OverrIDe public voID onLayoutChange(VIEw v,int left,int top,int right,int bottom,int oldleft,int oldtop,int oldRight,int oldBottom) { /* old是改变前的左上右下坐标点值,没有old的是改变后的左上右下坐标点值 现在认为只要控件将Activity向上推的高度超过了1/3屏幕高,就认为软键盘d起*/ if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) { VIEwGroup.LayoutParams params = iv_login_logo.getLayoutParams();//获取布局,设置键盘d起后logo的宽高 params.height = 300; params.wIDth = 300; iv_login_logo.setLayoutParams(params); linearLayout.LayoutParams lp = new linearLayout.LayoutParams(ll_login_logobg.getLayoutParams()); lp.setmargins(0,90,50);//设置包含logo的布局的位置 ll_login_logobg.setLayoutParams(lp); } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {//键盘收回后,logo恢复原来大小,位置同样回到初始位置 VIEwGroup.LayoutParams params = iv_login_logo.getLayoutParams(); params.height = 480; params.wIDth = 480; iv_login_logo.setLayoutParams(params); linearLayout.LayoutParams lp = new linearLayout.LayoutParams(ll_login_logobg.getLayoutParams()); lp.setmargins(0,270,150); ll_login_logobg.setLayoutParams(lp); } } } 以上所述是小编给大家介绍的AndroID实现登陆页logo随键盘收放动态伸缩(完美解决键盘d出遮挡控件的问题),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android实现登陆页logo随键盘收放动态伸缩(完美解决键盘d出遮挡控件的问题)全部内容,希望文章能够帮你解决Android实现登陆页logo随键盘收放动态伸缩(完美解决键盘d出遮挡控件的问题)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)