欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于Android輸入法彈窗bug的優(yōu)雅處理

 更新時間:2021年10月27日 11:33:09   作者:Android唐浮  
在Android應(yīng)用中,當(dāng)跳轉(zhuǎn)到某個Activity時,該Activity顯示頁面的EditText獲得焦點,在某些機器中會觸發(fā)軟鍵盤的自動彈出,這篇文章主要給大家介紹了關(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)文章

最新評論