解決EditText被軟盤遮擋和鍵盤彈出佈局不上移
一開始我遇到過這個也在網上找了很多例子,都是在說在AndroidManifest檔案對應activity中加上
android:windowSoftInputMode=”stateVisible|adjustPan”這屬性就可以了,但是這個會有包含一個問題那就是佈局會上移,那麼如果你的頂上佈局是一個視屏播放鍵或者其他的,那麼就會看不見,導致使用者體驗度不好。如果不設定這個屬性的話,那麼就會出現你的EidtText被輸入法擋住,當用戶輸入文字的時候看不見輸入的字,導致使用者體驗度也不好,所以這裡給出我自己的解決思路,希望對你有所幫助。
解決思路:1.如果你的頭佈局可以選擇可要可不要的情況你可一直在你的AndroidManifest對應的activity中加入android:windowSoftInputMode=”stateVisible|adjustPan”這段程式碼就可以解決Edittext被擋住的問題,
2.不想整體佈局上移,當時又要Eidttext在輸入法的上面,那麼用上面的方法是不可能的了,這裡我的解決思路是,算出鍵盤的高度,然後根據這個來整體設定包裹EidtText的父view的marginBottom就可以解決這個問題,話不多說直接上程式碼:
<LinearLayout android:id="@+id/lyo_content" android:background="@color/white" android:layout_marginTop="@dimen/dp_4" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="45dp"> <EditText android:id="@+id/edit_content" android:layout_width="0dp" android:textColor="@color/tv_222222" android:textSize="14sp" android:theme="@style/myEidtText" android:layout_marginLeft="@dimen/dp_10" android:layout_weight="1" android:layout_height="match_parent" /> <com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton android:id="@+id/btn_send_msg" app:qmui_radius="2dp" app:qmui_backgroundColor="@color/login_btn_2ecc71" app:qmui_borderColor="@color/tv_3cee87" android:padding="10dp" android:textSize="13sp" android:textColor="@color/white" android:layout_marginRight="@dimen/dp_10" android:layout_marginLeft="@dimen/dp_10" android:text="@string/text29" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
這裡我只是給出部分xml檔案中的程式碼,其實在這個LinearLayout上可能還有很多的佈局,反正不管你的佈局怎麼樣都是一個樣子那就是你的輸入框是在佈局的下面,在手機上顯示的話就是在手機螢幕的下方。
3.然後獲取到這個LinearLayout的控制元件,為什麼要獲取到這個控制元件呢,原因就是Edittext在這個view的包裹中,所以我只要算出鍵盤的高度來直接設定這個LinearLayout的marginBottom不就可以讓EidtText永遠在輸入法的上面了嗎?鍵盤消失直接他的marginBottom為0就可以了下面看程式碼。
4獲取輸入法的高度獲取鍵盤高度的時候請把對應的activity中的android:windowSoftInputMode=”stateVisible|adjustPan屬性刪除掉可能獲取不到鍵盤的高度哦儘量(沒有測過),把你的佈局檔案的跟佈局設定為RelativeLayout否則設定在外邊距的時候也會使佈局變形
View decorView = getActivity().getWindow().getDecorView();
//這個可以獲取鍵盤的高度
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
//計算出可見螢幕的高度
int displayHight = rect.bottom - rect.top;
//獲取螢幕整體高度
int height = decorView.getHeight();
//判斷鍵盤是否顯示和消失
boolean visible = (double) displayHight / height < 0.8;
int statusBarHeight = 0;
//獲取鍵盤的輸入法工具條的高度,否則設定的lyoContent的高度將會多出一點
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = getContext().getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
keyboardHeight = height - displayHight-statusBarHeight;
onSoftKeyBoardVisible(visible, keyboardHeight);
}
});
//獲取鍵盤的高度
}
private void onSoftKeyBoardVisible(boolean visible, int keyboardHeight) {
//如果鍵盤顯示那麼獲取到他的高度設定lyoContent的marginBottom 這裡lyoContent就是包裹EditText的那個view
if (visible){
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0,0,keyboardHeight);
lyoContent.setLayoutParams(lp);
}else {
//消失直接設定lyoContent的marginBottom 為0讓其恢復到原來的佈局
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0,0,0);
lyoContent.setLayoutParams(lp);
}
}
在這裡差不多可以解決佈局上移的問題了,但是這裡也有一個不好地方就是這種做法導致介面在不斷回撥OnGlobalLayoutListener方法,很不好,如果不考慮的這種方法的話,我這裡有一個巧妙的方法,就是在你的EidtText佈局下方設定一個高度和軟盤一樣的高度的View開始隱藏,通過監聽軟盤的顯示和消失相應隱藏或者顯示這個 View,Edittext也會出現在軟盤上方了。
ok!程式碼就這麼多希望多你有所幫助