關(guān)于Android輸入法彈窗bug的優(yōu)雅處理
前言
最近發(fā)現(xiàn)一個(gè)bug,在項(xiàng)目中的某個(gè)界面,每當(dāng)彈出輸入法時(shí),背景總是隨著輸入法上移,導(dǎo)致背景被壓縮,雖然不打緊,但發(fā)現(xiàn)這個(gè)bug之后極其不愉快。
別人家的產(chǎn)品處理
隨手拿了一部手機(jī)舉例
搜索框應(yīng)該在頂部,這樣即使彈出輸入法也不會(huì)遮擋

掘金評(píng)論類(lèi)似的輸入框在底部,輸入內(nèi)容時(shí),輸入框跟隨輸入法上移,背景不動(dòng)

wechat聊天界面,背景不動(dòng),輸入框和聊天記錄隨著輸入法上移


這三種情況基本上涵蓋了大部分包含輸入框的場(chǎng)景,所以接下來(lái)我們看怎么實(shí)現(xiàn)它們。
實(shí)現(xiàn)
輸入框在頂部的代碼就不講了。
掘金的輸入框彈窗實(shí)現(xiàn)
大家能夠很簡(jiǎn)單的看出掘金的彈窗是比較復(fù)雜的,還可以插入圖片和表情。
并且在彈窗出來(lái)之后,原本的文章是不可以滑動(dòng)的。
由此可以判斷出掘金評(píng)論的彈窗是個(gè)Dialog,并且是Dialog主題的Activity
那我們來(lái)驗(yàn)證一下。
這個(gè)彈窗的Activity叫 TransparentCommentActivityNew 對(duì)不對(duì)?
文章詳情的Activity叫 DetailActivity 對(duì)不對(duì)?
@掘金安卓開(kāi)發(fā)人員
這樣就很簡(jiǎn)單了,當(dāng)出現(xiàn)了新的Activity后,就無(wú)須考慮原先Activity背景壓縮的問(wèn)題,布局適配的問(wèn)題。
因?yàn)橐磺卸际窃谛碌腁ctivity中進(jìn)行,并且操作不了下面的Activity。
總結(jié):新建一個(gè)彈窗主題的Activity可以簡(jiǎn)單方便的解決輸入法引起的布局錯(cuò)亂問(wèn)題。
weChat聊天背景不會(huì)被壓縮的問(wèn)題
我遇到了一個(gè)問(wèn)題解決了很久。就是正常情況的布局,背景是會(huì)被輸入法壓縮的。
看代碼:
<?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="請(qǐng)輸入..." />
<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"
動(dòng)態(tài)計(jì)算輸入框的高度,在輸入法彈窗彈出時(shí)給EditText下方增加一個(gè)高度相等的View,輸入法消失時(shí)消失該View。
總結(jié)
到此這篇關(guān)于Android輸入法彈窗bug的優(yōu)雅處理的文章就介紹到這了,更多相關(guān)Android輸入法彈窗處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中自定義ContentProvider實(shí)例
應(yīng)用A(TestBaidu)調(diào)用另外一個(gè)應(yīng)用(TestContentProvider)中的自定義ContentProvider,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android通過(guò)Java sdk的方式接入OpenCv的方法
這篇文章主要介紹了Android通過(guò)Java sdk的方式接入OpenCv的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
3種Android隱藏頂部狀態(tài)欄及標(biāo)題欄的方法
這篇文章主要為大家詳細(xì)介紹了3種Android隱藏頂部狀態(tài)欄及標(biāo)題欄的方法,還涉及一種隱藏Android 4.0平板底部狀態(tài)欄的方法,感興趣的小伙伴們可以參考一下2016-02-02
Android編程實(shí)現(xiàn)向SD卡寫(xiě)入數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)向SD卡寫(xiě)入數(shù)據(jù)的方法,涉及Android針對(duì)SD卡狀態(tài)判斷,文件及權(quán)限操作等相關(guān)技巧,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)簡(jiǎn)單MD5加密的方法
這篇文章主要介紹了Android實(shí)現(xiàn)簡(jiǎn)單MD5加密的方法,涉及Android編碼操作及加密的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
Android編程程序?qū)崿F(xiàn)一鍵鎖屏的方法講解
今天小編就為大家分享一篇關(guān)于Android編程程序?qū)崿F(xiàn)一鍵鎖屏的方法講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Android Studio4.0導(dǎo)入OpenCv4.3.0的方法步驟
這篇文章主要介紹了Android Studio4.0導(dǎo)入OpenCv4.3.0的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
在Android系統(tǒng)源碼中預(yù)置APK的方法
今天小編就為大家分享一篇關(guān)于在Android系統(tǒng)源碼中預(yù)置APK的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12

