監聽鍵盤彈起,防止底部布局上移

2021-10-23 07:26:57 字數 4665 閱讀 5767

最近做需求,遇到軟鍵盤彈起底部布局按鈕被頂起的情況,需求不一樣,對應方法也會存在差異性,說說我遇到的問題及解決過程。

登入頁根布局是relativelayout,底部有乙個登入按鈕通過android:layout_alignparentbottom="true"放至最底部,軟鍵盤彈起時按鈕會被頂到鍵盤上,遮住輸入框,體驗巨差。

1.在manifest裡面加入鍵盤屬性android:windowsoftinputmode="adjustpan」,關於它的引數

詳情可以看看這篇文章雖然對我沒什麼用,不過還是記錄下,萬一對其他人有用呢。

2.布局裡面加入scrollerview,將要顯示在軟鍵盤之上的布局包裹起來,這樣軟鍵盤會一直在要顯示的布局下方,但底部按鈕不會跟著上移。親測在部分機型上有用,但是有個華為手機任然無效。

3.無奈只能監聽軟鍵盤的彈起跟收入了,我是這麼操作的,上**

/**

* 監聽鍵盤彈起,防止鍵盤頂起底部布局

*/private

void

setlayoutkayboard()

else},

10);}

}});

}

我這裡用到了乙個軟鍵盤工具類keyboardutils:

public

final

class

keyboardutils

/** * show the soft input.

** @param activity the activity.

*/public

static

void

showsoftinput

(final activity activity)

imm.

showsoftinput

(view, inputmethodmanager.show_forced);}

/** * show the soft input.

** @param view the view.

*/public

static

void

showsoftinput

(final view view)

/** * hide the soft input.

** @param activity the activity.

*/public

static

void

hidesoftinput

(final activity activity)

/** * hide the soft input.

** @param view the view.

*/public

static

void

hidesoftinput

(final view view)

/** * toggle the soft input display or not.

*/public

static

void

togglesoftinput()

/** * return whether soft input is visible.

* the minimum height is 200

** @param activity the activity.

* @return : yes

: no

*/public

static

boolean

issoftinputvisible

(final activity activity)

/** * return whether soft input is visible.

** @param activity the activity.

* @param minheightofsoftinput the minimum height of soft input.

* @return : yes

: no

*/public

static

boolean

issoftinputvisible

(final activity activity,

final

int minheightofsoftinput)

private

static

intgetcontentviewinvisibleheight

(final activity activity)

/** * register soft input changed listener.

** @param activity the activity.

* @param listener the soft input changed listener.

*/public

static

void

registersoftinputchangedlistener

(final activity activity,

final onsoftinputchangedlistener listener)

final view contentview = activity.

findviewbyid

(android.r.id.content)

; scontentviewinvisibleheightpre =

getcontentviewinvisibleheight

(activity)

; onsoftinputchangedlistener = listener;

ongloballayoutlistener =

newongloballayoutlistener()

}}};

contentview.

getviewtreeobserver()

.addongloballayoutlistener

(ongloballayoutlistener);}

/** * register soft input changed listener.

** @param activity the activity.

*/@targetapi

(build.version_codes.jelly_bean)

public

static

void

unregistersoftinputchangedlistener

(final activity activity)

/** * fix the leaks of soft input.

* call the function in .

** @param context the context.

*/public

static

void

fixsoftinputleaks

(final context context)

;for

(int i =

0; i <

3; i++

) object obj = declaredfield.

get(imm);if

(obj == null ||

!(obj instanceof

view))

continue

; view view =

(view) obj;

if(view.

getcontext()

== context)

else

}catch

(throwable th)}}

/** * click blankj area to hide soft input.

* copy the following code in ur activity.

*/public

static

void

clickblankarea2hidesoftinput()

}return super.dispatchtouchevent(ev);

}// return whether touch the view.

private boolean isshouldhidekeyboard(view v, motionevent event) ;

v.getlocationinwindow(l);

int left = l[0],

top = l[1],

bottom = top + v.getheight(),

right = left + v.getwidth();

return !(event.getx() > left && event.getx() < right

&& event.gety() > top && event.gety() < bottom);

}return false;}*/

}public

inte***ce

onsoftinputchangedlistener

}

記錄一下為了防止下次遇到又忘記或者查詢半天無果浪費時間。

android 監聽鍵盤的彈起和隱藏

在layout的根布局用自定義的,重寫onlayout方法的keyboardlayout public class keyboardlayout extends relativelayout public keyboardlayout context context,attributeset att...

angular監聽移動端鍵盤的彈起和收回

頁面的提交按鈕採用的是固定定位在頁面的底部,鍵盤彈出後,提交按鈕緊挨著鍵盤的上方,輸入框獲得焦點後,鍵盤彈出,並且輸入框回自動定位上方的空白處,此時由於鍵盤上方固定定位的提交按鈕的原因有可能會遮擋住獲得焦點的輸入框,從而導致使用者看不見輸入框,需要使用者手動滑動螢幕,移動輸入框的位置,才可以進行輸入...

h5 移動端 監聽軟鍵盤彈起 收起

回車確認 btn on keypress function e document keyup function e 1.在ios中軟鍵盤彈起時,僅會引起 body scrolltop值改變,但是我們可以通過輸入框的獲取焦點情況來做判斷,但也只能在ios中採用這個方案,因為在android中存在主動收...