您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關android應用中程序虛擬鍵盤彈出遮擋登陸按鈕如何解決,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
代碼實現如下;
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應用中程序虛擬鍵盤彈出遮擋登陸按鈕如何解決,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。