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

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

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

@掘金安卓開發(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)文章

最新評(píng)論