android 實(shí)現(xiàn)按鈕浮動(dòng)在鍵盤上方的實(shí)例代碼
大家好,我是夢辛工作室的靈,最近在幫客戶修改安卓程序時(shí),有要求到一個(gè)按鈕要浮動(dòng)在鍵盤的上方,下面大概講一下實(shí)現(xiàn)方法:
其實(shí)很簡單,分三步走
第一步 獲取當(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,很好用,下面是代碼
/**
* 夢辛靈 實(shí)現(xiàn)按鈕浮動(dòng)工具
*/
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android實(shí)現(xiàn)支付寶螞蟻森林水滴浮動(dòng)效果
- Android實(shí)現(xiàn)圖片浮動(dòng)隨意拖拽效果
- Android 浮動(dòng)編輯框的具體實(shí)現(xiàn)代碼
- Android利用浮動(dòng)窗口提示用戶操作
- 安卓(android)仿電商app商品詳情頁按鈕浮動(dòng)效果
- Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動(dòng)效果
- Android應(yīng)用中制作選中后圖標(biāo)變大浮動(dòng)效果的代碼分享
- Android浮動(dòng)窗口實(shí)現(xiàn)原理及代碼實(shí)例
相關(guān)文章
Android如何自定義View實(shí)現(xiàn)橫向的雙水波紋進(jìn)度條
最近有個(gè)需求需要實(shí)現(xiàn)自定義加載進(jìn)度條,于是深入研究了一下,這篇文章主要給大家介紹了關(guān)于Android如何自定義View實(shí)現(xiàn)橫向的雙水波紋進(jìn)度條的相關(guān)資料,需要的朋友可以參考下2021-11-11
基于RecyclerView實(shí)現(xiàn)橫向GridView效果
這篇文章主要為大家詳細(xì)介紹了基于RecyclerView實(shí)現(xiàn)橫向GridView效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android中關(guān)于CoordinatorLayout的一些實(shí)用布局技巧
大家都知道CoordinatorLayout是一個(gè)“加強(qiáng)版”的 FrameLayout,那么下面這篇文章主要給大家分享了Android中關(guān)于CoordinatorLayout的一些布局技巧,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
Android自定義view 你所需要知道的基本函數(shù)總結(jié)
這篇文章主要介紹了Android自定義view 你所需要知道的基本函數(shù)的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android Loop機(jī)制中Looper與handler詳細(xì)分析
Handler是Android線程之間的消息機(jī)制,主要的作用是將一個(gè)任務(wù)切換到指定的線程中去執(zhí)行,準(zhǔn)確的說是切換到構(gòu)成Handler的looper所在的線程中去出處理。本文將詳細(xì)介紹Android Handler機(jī)制和Looper Handler Message關(guān)系2022-11-11
android 處理配置變更的實(shí)現(xiàn)方法
這篇文章主要介紹了android 處理配置變更的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
Android 自定義SeekBar 實(shí)現(xiàn)分段顯示不同背景顏色的示例代碼
這篇文章主要介紹了Android 自定義SeekBar 實(shí)現(xiàn)分段顯示不同背景顏色,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Android實(shí)現(xiàn)圖片浮動(dòng)隨意拖拽效果
這篇文章主要介紹了Android的圖片在界面隨意拖動(dòng)的功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過來看看吧2018-04-04

