android 實現(xiàn)按鈕浮動在鍵盤上方的實例代碼
大家好,我是夢辛工作室的靈,最近在幫客戶修改安卓程序時,有要求到一個按鈕要浮動在鍵盤的上方,下面大概講一下實現(xiàn)方法:
其實很簡單,分三步走
第一步 獲取當前屏幕的高度
Display defaultDisplay = mcontext.getWindowManager().getDefaultDisplay(); Point point = new Point(); defaultDisplay.getSize(point); height = point.y;
第二步 獲取當前屏幕可見區(qū)域的高度,用于判斷當前鍵盤是否隱藏或顯示
public void setFloatView(View root,View floatview){ this.root = root; //根節(jié)點 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){ //鍵盤顯示 }else{ //鍵盤隱藏 } } }; root.getViewTreeObserver().addOnGlobalLayoutListener(listener); }
第三步 當鍵盤隱藏時讓按鈕 動畫移動至原有位置,當前鍵盤顯示時讓按鈕動畫移動至當前鍵盤的高度上方
if(isKeyboardShowing){ //鍵盤顯示 floatview.animate().translationY(-heightDifference).setDuration(0).start(); }else{ //鍵盤隱藏 floatview.animate().translationY(0).start(); }
然后我為了方便封裝了一個工具類 FloatBtnUtil,很好用,下面是代碼
/** * 夢辛靈 實現(xiàn)按鈕浮動工具 */ 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é)點 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 實現(xiàn)按鈕浮動在鍵盤上方的文章就介紹到這了,更多相關(guān)android 實現(xiàn)按鈕浮動在鍵盤上方內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android數(shù)據(jù)庫greenDAO配置與使用介紹
這篇文章主要介紹了Android集成GreenDao數(shù)據(jù)庫,使用數(shù)據(jù)庫存儲時候,一般都會使用一些第三方ORM框架,比如GreenDao,本文分幾步給大家介紹Android集成GreenDao數(shù)據(jù)庫的方法,需要的朋友可以參考下2023-03-03Android GZip的使用-開發(fā)中網(wǎng)絡(luò)請求的壓縮實例詳解
這篇文章主要介紹了Android GZip的使用-開發(fā)中網(wǎng)絡(luò)請求的壓縮實例詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11android實現(xiàn)關(guān)閉或開啟移動網(wǎng)絡(luò)數(shù)據(jù)
本篇文章是對android實現(xiàn)關(guān)閉或開啟移動網(wǎng)絡(luò)數(shù)據(jù)進行了詳細的分析介紹,需要的朋友參考下2013-06-06Kotlin字節(jié)碼層探究構(gòu)造函數(shù)與成員變量和init代碼塊執(zhí)行順序
這篇文章主要介紹了字節(jié)碼層Kotlin構(gòu)造函數(shù)與成員變量和init代碼塊執(zhí)行順序,kotlin里面的構(gòu)造函數(shù)分為主構(gòu)造函數(shù)和次構(gòu)造函數(shù)。主構(gòu)造函數(shù)只能有一個,次構(gòu)造函數(shù)個數(shù)不限制,可以有一個或者多個2022-11-11Android FrameWork之Zygote啟動示例詳解
這篇文章主要為大家介紹了Android FrameWork之Zygote啟動示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07Android使用AsyncQueryHandler實現(xiàn)獲取手機聯(lián)系人功能
這篇文章主要為大家詳細介紹了Android使用AsyncQueryHandler實現(xiàn)獲取手機聯(lián)系人功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android 啟動模式FLAG_ACTIVITY_CLEAR_TOP案例詳解
這篇文章主要介紹了Android 啟動模式FLAG_ACTIVITY_CLEAR_TOP案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08Android編程實現(xiàn)Listview點擊展開和隱藏的方法
這篇文章主要介紹了Android編程實現(xiàn)Listview點擊展開和隱藏的方法,涉及Android中Listview的響應(yīng)點擊與樣式變換相關(guān)操作技巧,需要的朋友可以參考下2015-12-12