在Android上打开键盘时如何隐藏广告横幅

在Android上打开键盘时如何隐藏广告横幅,第1张

概述我将MoPubView用于我的应用中的广告.但是,当我打开键盘时,该横幅会升起并关闭小部件.这是来自布局的代码:<FrameLayoutxmlns:android="http://schemas.android.com/apkes/android"xmlns:app="http://schemas.android.com/apkes-auto"android:id="@+id/container

我将MoPubVIEw用于我的应用中的广告.但是,当我打开键盘时,该横幅会升起并关闭小部件.

这是来自布局的代码:

 <FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    androID:ID="@+ID/container"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:background="@color/material_bg">    <ScrollVIEw        androID:ID="@+ID/scroll"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent">        <linearLayout            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:orIEntation="vertical"            androID:paddingBottom="@dimen/activity_horizontal_margin">            ...Content here        </linearLayout>    </ScrollVIEw>    <com.mopub.mobileads.MoPubVIEw        androID:ID="@+ID/advIEw"        androID:layout_wIDth="fill_parent"        androID:layout_height="@dimen/mopub_height"        androID:layout_gravity="bottom"/></FrameLayout>

此布局的此方案:


如何隐藏此横幅或将其固定在底部?

解决方法:

创建一个类来处理键盘检测,

import androID.graphics.Rect;import androID.vIEw.VIEw;import androID.vIEw.VIEwTreeObserver;import java.util.linkedList;import java.util.List;public class SoftKeyboardStateWatcher implements VIEwTreeObserver.OnGlobalLayoutListener {    public interface SoftKeyboardStateListener {        voID onSoftKeyboardOpened(int keyboardHeightInPx);        voID onSoftKeyboardClosed();    }    private final List<SoftKeyboardStateListener> Listeners = new linkedList<SoftKeyboardStateListener>();    private final VIEw activityRootVIEw;    private int        lastSoftKeyboardHeightInPx;    private boolean    isSoftKeyboardOpened;    public SoftKeyboardStateWatcher(VIEw activityRootVIEw) {        this(activityRootVIEw, false);    }    public SoftKeyboardStateWatcher(VIEw activityRootVIEw, boolean isSoftKeyboardOpened) {        this.activityRootVIEw     = activityRootVIEw;        this.isSoftKeyboardOpened = isSoftKeyboardOpened;        activityRootVIEw.getVIEwTreeObserver().addOnGlobalLayoutListener(this);    }    @OverrIDe    public voID onGlobalLayout() {        final Rect r = new Rect();        //r will be populated with the coordinates of your vIEw that area still visible.        activityRootVIEw.getwindowVisibledisplayFrame(r);        final int heightDiff = activityRootVIEw.getRootVIEw().getHeight() - (r.bottom - r.top);        if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...            isSoftKeyboardOpened = true;            notifyOnSoftKeyboardOpened(heightDiff);        } else if (isSoftKeyboardOpened && heightDiff < 100) {            isSoftKeyboardOpened = false;            notifyOnSoftKeyboardClosed();        }    }    public voID setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {        this.isSoftKeyboardOpened = isSoftKeyboardOpened;    }    public boolean isSoftKeyboardOpened() {        return isSoftKeyboardOpened;    }    /**     * Default value is zero {@code 0}.     *     * @return last saved keyboard height in px     */    public int getLastSoftKeyboardHeightInPx() {        return lastSoftKeyboardHeightInPx;    }    public voID addSoftKeyboardStateListener(SoftKeyboardStateListener Listener) {        Listeners.add(Listener);    }    public voID removeSoftKeyboardStateListener(SoftKeyboardStateListener Listener) {        Listeners.remove(Listener);    }    private voID notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;        for (SoftKeyboardStateListener Listener : Listeners) {            if (Listener != null) {                Listener.onSoftKeyboardOpened(keyboardHeightInPx);            }        }    }    private voID notifyOnSoftKeyboardClosed() {        for (SoftKeyboardStateListener Listener : Listeners) {            if (Listener != null) {                Listener.onSoftKeyboardClosed();            }        }    }}

在您的AdActivity的onCreate方法中,插入以下行:

final SoftKeyboardStateWatcher softKeyboardStateWatcher = new SoftKeyboardStateWatcher(findVIEwByID(R.ID.container);        // Add Listener        softKeyboardStateWatcher.addSoftKeyboardStateListener(new SoftKeyboardStateWatcher.softKeyboardStateListener() {                @OverrIDe                public voID onSoftKeyboardOpened(int keyboardHeightInPx) {                }                @OverrIDe                public voID onSoftKeyboardClosed() {                }            });        // then just handle callbacks
总结

以上是内存溢出为你收集整理的在Android上打开键盘时如何隐藏广告横幅全部内容,希望文章能够帮你解决在Android上打开键盘时如何隐藏广告横幅所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1079479.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存