新聞中心
本篇文章重點(diǎn)為大家講解一下通過android實(shí)現(xiàn)按鈕浮動(dòng)在鍵盤上方具體方法,有需要的小伙伴可以參考一下。

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供曲靖網(wǎng)站建設(shè)、曲靖做網(wǎng)站、曲靖網(wǎng)站設(shè)計(jì)、曲靖網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、曲靖企業(yè)網(wǎng)站模板建站服務(wù),10多年曲靖做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
第一步
獲取當(dāng)前屏幕的高度
Display defaultDisplay = mcontext.getWindowManager().getDefaultDisplay();
Point point = new Point();
defaultDisplay.getSize(point);
height = point.y;
第二步
獲取當(dāng)前屏幕可見區(qū)域的高度,用于判斷當(dāng)前鍵盤是否隱藏或顯示
public void setFloatView(View root,View floatview){
this.root = root; //根節(jié)點(diǎn)
listener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
mcontext.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
int heightDifference = height - (r.bottom - r.top); // 實(shí)際高度減去可視圖高度即是鍵盤高度
boolean isKeyboardShowing = heightDifference > height / 3;
if(isKeyboardShowing){
//鍵盤顯示
}else{
//鍵盤隱藏
}
}
};
root.getViewTreeObserver().addOnGlobalLayoutListener(listener);
}
第三步
當(dāng)鍵盤隱藏時(shí)讓按鈕 動(dòng)畫移動(dòng)至原有位置,當(dāng)前鍵盤顯示時(shí)讓按鈕動(dòng)畫移動(dòng)至當(dāng)前鍵盤的高度上方
if(isKeyboardShowing){
//鍵盤顯示
floatview.animate().translationY(-heightDifference).setDuration(0).start();
}else{
//鍵盤隱藏
floatview.animate().translationY(0).start();
}
為了方便封裝了一個(gè)工具類 FloatBtnUtil
public class FloatBtnUtil {
private static int height = 0;
private Activity mcontext;
private ViewTreeObserver.OnGlobalLayoutListener listener;
private View root;
public FloatBtnUtil(Activity mcontext){
this.mcontext = mcontext;
if (height == 0){
Display defaultDisplay = mcontext.getWindowManager().getDefaultDisplay();
Point point = new Point();
defaultDisplay.getSize(point);
height = point.y;
}
}
public void setFloatView(View root,View floatview){
this.root = root; //視圖根節(jié)點(diǎn) floatview // 需要顯示在鍵盤上的View組件
listener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
mcontext.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
int heightDifference = height - (r.bottom - r.top);
boolean isKeyboardShowing = heightDifference > height / 3;
if(isKeyboardShowing){
floatview.animate().translationY(-heightDifference).setDuration(0).start();
}else{
floatview.animate().translationY(0).start();
}
}
};
root.getViewTreeObserver().addOnGlobalLayoutListener(listener);
}
public void clearFloatView(){
if (listener != null && root != null)
root.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
}
下面是使用代碼:
private void initFloatBtn() {
FloatBtnUtil floatBtnUtil = new FloatBtnUtil(this);
LinearLayout lin_bottom = (LinearLayout) this.findViewById(R.id.lin_bottom);
LinearLayout lin_root = (LinearLayout)this.findViewById(R.id.lin_root);
floatBtnUtil.setFloatView(lin_root,lin_bottom);
}
總結(jié)
到此這篇關(guān)于android 實(shí)現(xiàn)按鈕浮動(dòng)在鍵盤上方的文章就介紹到這了,更多相關(guān)android 實(shí)現(xiàn)按鈕浮動(dòng)在鍵盤上方內(nèi)容
新聞名稱:通過android實(shí)現(xiàn)按鈕浮動(dòng)在鍵盤上方
路徑分享:http://fisionsoft.com.cn/article/dhsjpii.html


咨詢
建站咨詢
