软件编程
位置:首页>> 软件编程>> Android编程>> android虚拟键盘弹出遮挡登陆按钮问题的解决方法

android虚拟键盘弹出遮挡登陆按钮问题的解决方法

作者:wangwo1991  发布时间:2022-03-06 15:54:54 

标签:android,虚拟键盘,遮挡按钮

Android虚拟键盘的弹起会遮挡住部分ui,虽然通过在清单文件中设置,可以随着虚拟键盘的弹出,布局往上推,但是面对登陆界面时,并没有太大的作用,这样就会导致用户体验不好;开发中既然出现了就的解决;先说先解决的思路:获取到屏幕的高度、虚拟键盘的高度,布局的高度,用屏幕的高度减去布局的高度,用高度差和虚拟键盘的高度进行对比;代码实现如下;


private LinearLayout logo_layout;
 private ImageView iv_logo;
 private int sh;
 private int layoutH;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   logo_layout=(LinearLayout) findViewById(R.id.logo_layout);
   iv_logo=(ImageView) findViewById(R.id.iv_logo);
   //获取屏幕的高度
   DisplayMetrics dm = new DisplayMetrics();
   WindowManager windowMgr = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
   windowMgr.getDefaultDisplay().getMetrics(dm);
   sh = dm.heightPixels;
   logo_layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

// 当layout执行结束后回调此方法
     @Override
     public void onGlobalLayout() {
       logo_layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
       layoutH = logo_layout.getHeight();
     }
   });
   //当键盘弹起的时候用屏幕的高度减去布局的高度,同时获取到键盘的高度,用键盘的高度和剩余的高度做对比
   SoftKeyBoardListener.setListener(MainActivity.this, new OnSoftKeyBoardChangeListener() {

@Override
     public void keyBoardShow(int height) {
       //键盘弹起回调
       int h=sh-layoutH;
       if(h>height){//高度大于键盘的高度
         setLayoutH(80);
       }else{
         //高度小于键盘的高度
         int resetH=Math.abs(height+layoutH-sh);
         setLayoutH(resetH);
       }
     }

@Override
     public void keyBoardHide(int height) {
       //键盘隐藏回调
       setLayoutH(80);
     }
   });  
 }
 /**
  * 重新设置布局高度
  */
 private void setLayoutH(int h){
   LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) iv_logo.getLayoutParams();
   layoutParams.topMargin=dip2px(MainActivity.this, h);
   iv_logo.setLayoutParams(layoutParams);
 }
 /**
  * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
  */
 public static int dip2px(Context context,float dpValue) {
   final float scale =context.getResources().getDisplayMetrics().density;
   return (int) (dpValue * scale + 0.5f);
 }


private View rootView;//activity的根视图
 int rootViewVisibleHeight;//纪录根视图的显示高度
 private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;

public SoftKeyBoardListener(Activity activity) {
   //获取activity的根视图
   rootView = activity.getWindow().getDecorView();

//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
   rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
     @Override
     public void onGlobalLayout() {
       //获取当前根视图在屏幕上显示的大小
       Rect r = new Rect();
       rootView.getWindowVisibleDisplayFrame(r);
       int visibleHeight = r.height();
       if (rootViewVisibleHeight == 0) {
         rootViewVisibleHeight = visibleHeight;
         return;
       }

//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
       if (rootViewVisibleHeight == visibleHeight) {
         return;
       }

//根视图显示高度变小超过200,可以看作软键盘显示了
       if (rootViewVisibleHeight - visibleHeight > 200) {
         if (onSoftKeyBoardChangeListener != null) {
           onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
         }
         rootViewVisibleHeight = visibleHeight;
         return;
       }

//根视图显示高度变大超过200,可以看作软键盘隐藏了
       if (visibleHeight - rootViewVisibleHeight > 200) {
         if (onSoftKeyBoardChangeListener != null) {
           onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
         }
         rootViewVisibleHeight = visibleHeight;
         return;
       }

}
   });
 }

private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
   this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
 }

public interface OnSoftKeyBoardChangeListener {
   void keyBoardShow(int height);

void keyBoardHide(int height);
 }

public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
   SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
   softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
 }

以上做了仔细说明了,运行效果如下:

android虚拟键盘弹出遮挡登陆按钮问题的解决方法

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com