android虛擬鍵盤彈出遮擋登陸按鈕問題的解決方法
Android虛擬鍵盤的彈起會遮擋住部分ui,雖然通過在清單文件中設置,可以隨著虛擬鍵盤的彈出,布局往上推,但是面對登陸界面時,并沒有太大的作用,這樣就會導致用戶體驗不好;開發(fā)中既然出現(xiàn)了就的解決;先說先解決的思路:獲取到屏幕的高度、虛擬鍵盤的高度,布局的高度,用屏幕的高度減去布局的高度,用高度差和虛擬鍵盤的高度進行對比;代碼實現(xiàn)如下;
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執(zhí)行結束后回調此方法
@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);
}
/**
* 根據(jù)手機的分辨率從 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();
//監(jiān)聽視圖樹中全局布局發(fā)生改變或者視圖樹中的某個視圖的可視狀態(tài)發(fā)生改變
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;
}
//根視圖顯示高度沒有變化,可以看作軟鍵盤顯示/隱藏狀態(tài)沒有改變
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中使用Theme來解決啟動app時出現(xiàn)的空白屏問題
相信大多數(shù)人一開始都會對啟動app的時候出現(xiàn)先白瓶或者黑屏然后才進入第一個界面,例如:SplashActivity。那這是什么原因造成的呢?下面小編給大家介紹下2016-12-12
Android程序開發(fā)通過HttpURLConnection上傳文件到服務器
這篇文章主要介紹了Android程序開發(fā)通過HttpURLConnection上傳文件到服務器的相關資料,需要的朋友可以參考下2016-01-01
Android中給fragment寫入?yún)?shù)的輕量開發(fā)包FragmentArgs簡介
這篇文章主要介紹了Android中給fragment寫入?yún)?shù)的輕量開發(fā)包FragmentArgs簡介,需要的朋友可以參考下2014-10-10
android Setting中隱藏項實現(xiàn)原理與代碼
我們都知道做程序員有時會就像android中,程序員在setting中就隱藏這樣一項,接下來將詳細介紹,感興趣的朋友可以了解下哦2013-01-01
Android?中?FrameLayout?布局及屬性的使用詳解
這篇文章主要介紹了Android?中?FrameLayout?布局及屬性的使用,FrameLayout?在實現(xiàn)簡單布局時非常方便,特別適用于疊加式布局,如顯示疊加的圖層或浮動按鈕等,需要的朋友可以參考下2024-03-03
Android 通過當前經(jīng)緯度獲得城市的實例代碼
Android 通過當前經(jīng)緯度獲得城市的實例代碼,需要的朋友可以參考一下2013-06-06

