日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
通過android實(shí)現(xiàn)按鈕浮動(dòng)在鍵盤上方

本篇文章重點(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)容


網(wǎng)站欄目:通過android實(shí)現(xiàn)按鈕浮動(dòng)在鍵盤上方
本文URL:http://www.5511xx.com/article/dhsjpii.html