關(guān)于Android輸入法彈窗bug的優(yōu)雅處理
前言
最近發(fā)現(xiàn)一個bug,在項目中的某個界面,每當(dāng)彈出輸入法時,背景總是隨著輸入法上移,導(dǎo)致背景被壓縮,雖然不打緊,但發(fā)現(xiàn)這個bug之后極其不愉快。
別人家的產(chǎn)品處理
隨手拿了一部手機舉例
搜索框應(yīng)該在頂部,這樣即使彈出輸入法也不會遮擋
掘金評論類似的輸入框在底部,輸入內(nèi)容時,輸入框跟隨輸入法上移,背景不動
wechat聊天界面,背景不動,輸入框和聊天記錄隨著輸入法上移
這三種情況基本上涵蓋了大部分包含輸入框的場景,所以接下來我們看怎么實現(xiàn)它們。
實現(xiàn)
輸入框在頂部的代碼就不講了。
掘金的輸入框彈窗實現(xiàn)
大家能夠很簡單的看出掘金的彈窗是比較復(fù)雜的,還可以插入圖片和表情。
并且在彈窗出來之后,原本的文章是不可以滑動的。
由此可以判斷出掘金評論的彈窗是個Dialog,并且是Dialog主題的Activity
那我們來驗證一下。
這個彈窗的Activity叫 TransparentCommentActivityNew 對不對?
文章詳情的Activity叫 DetailActivity 對不對?
@掘金安卓開發(fā)人員
這樣就很簡單了,當(dāng)出現(xiàn)了新的Activity后,就無須考慮原先Activity背景壓縮的問題,布局適配的問題。
因為一切都是在新的Activity中進行,并且操作不了下面的Activity。
總結(jié):新建一個彈窗主題的Activity可以簡單方便的解決輸入法引起的布局錯亂問題。
weChat聊天背景不會被壓縮的問題
我遇到了一個問題解決了很久。就是正常情況的布局,背景是會被輸入法壓縮的。
看代碼:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/test"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="請輸入..." /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" /> </LinearLayout> </RelativeLayout>
效果圖(可以看到月亮被壓縮了):
解決方法
方法一
在AndroidManifest.xml文件里面的Activity配置:
android:windowSoftInputMode="adjustResize|stateHidden"
在onCreate方法中設(shè)置背景,替代xml中的背景:
getWindow().setBackgroundDrawableResource(R.mipmap.test);
方法二
在AndroidManifest.xml文件里面的Activity配置:
android:windowSoftInputMode="adjustResize|stateHidden"
2. 布局文件設(shè)置自定義背景
<com.myapplication.MyBackground android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/test" />
public class MyBackground extends RelativeLayout { private Context mContext; public MyBackground(Context context) { super(context); mContext = context; } public MyBackground(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; } public MyBackground(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mContext = context; } public MyBackground(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); mContext = context; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { DisplayMetrics dm = new DisplayMetrics(); WindowManager mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); mWm.getDefaultDisplay().getMetrics(dm); int screenHeight = dm.heightPixels; heightMeasureSpec = MeasureSpec.makeMeasureSpec(screenHeight, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
方法三
在AndroidManifest.xml文件里面的Activity配置:
android:windowSoftInputMode="adjustNothing|stateHidden"
動態(tài)計算輸入框的高度,在輸入法彈窗彈出時給EditText下方增加一個高度相等的View,輸入法消失時消失該View。
總結(jié)
到此這篇關(guān)于Android輸入法彈窗bug的優(yōu)雅處理的文章就介紹到這了,更多相關(guān)Android輸入法彈窗處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Retrofit下載文件并實現(xiàn)進度監(jiān)聽的示例
這篇文章主要介紹了使用Retrofit下載文件并實現(xiàn)進度監(jiān)聽的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Android Studio 2020新版本卡在Gradle downloading/sync failed/下載緩慢/
Android Studio 2020新版本 卡在Gradle downloading / sync failed / 下載緩慢 / 下載超時 親測有效解決辦法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-12-12Android開發(fā)使用json實現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能示例
這篇文章主要介紹了Android開發(fā)使用json實現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能,結(jié)合具體實例形式分析了Android使用json格式數(shù)據(jù)在服務(wù)器與客戶端傳遞實現(xiàn)數(shù)據(jù)庫查詢功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android開發(fā)之圖形圖像與動畫(一)Paint和Canvas類學(xué)習(xí)
Paint類代表畫筆,用來描述圖形的顏色和風(fēng)格,如線寬,顏色,透明度和填充效果等信息;Canvas類代表畫布,通過該類提供的構(gòu)造方法,可以繪制各種圖形;感興趣的朋友可以了解下啊,希望本文對你有所幫助2013-01-01Android Handler之消息循環(huán)的深入解析
本篇文章是對Handler消息循環(huán)進行了詳細的分析介紹,需要的朋友參考下2013-05-05android教程使用webview訪問https的url處理sslerror示例
這篇文章主要介紹了android教程使用webview訪問https的url處理sslerror示例,大家參考使用吧2014-01-01Android 使用Vitamio打造自己的萬能播放器(8)——細節(jié)優(yōu)化
本文主要介紹Android Vitamio開發(fā)播放器,這里給大家提供了一些小的細節(jié)優(yōu)化,更加完善播放器的功能,希望能幫助有需要的小伙伴2016-07-07