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

4

我在我的应用中使用MoPubView来投放广告。但是当我打开键盘时,该横幅会上升并关闭部件。

enter image description here

这是布局中的代码:

 <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>

这种布局的方案如下: enter image description here

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


我认为这个答案可能会对你有所帮助。请查看以下链接: https://dev59.com/qHA75IYBdhLWcg3w1sv2#26176575 - Milad Nouri
这似乎是最优雅的解决方案:https://dev59.com/qHA75IYBdhLWcg3w1sv2#26176575 - Rabi
3个回答

2
在AndroidManifest.xml中添加android:windowSoftInputMode="adjustPan"解决了这个问题。
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
android:theme="@style/AppTheme.NoActionBar"> 
</activity>

2
创建一个类来处理键盘检测。
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

@Asal Imam - 嗨,伙计,谢谢,但是 - http://joxi.ru/ZrJVlnVh4jwkrj - 我必须在这里设置哪个ID?如果我有很多用于输入的小部件(EditText)- 每个edittext都需要一个keyboardlistener吗? - Artem
你不需要在每个小部件上设置SoftKeyboardStateWatcher...!你只需要设置你的布局ID即可...需要示例吗? - Arsal Imam
1
@ArtemShevchenko ....!我更新了答案,现在你不需要更改id,只需要使用相同的id即可。 - Arsal Imam
1
你得到了期望的结果吗,兄弟? - Arsal Imam
我测试了一下,不起作用 - 当我启动Activity并将可见性设置为GONE时,即使我没有打开键盘。((( - Artem
我尝试了这个,但不起作用(我尝试使用所有的ID,但不起作用)。当活动开始时,我在onSoftKeyboardOpened()中看到了一个名为OPEN的Toast(我添加了Toast.makeText(...)),但之后没有任何反应。 - Artem

1

当您不想显示广告时,请使用以下代码

 adView.destroy();
 adView.setVisibility(View.GONE);

编辑:

尝试使用这个:

  android:windowSoftInputMode="adjustPan";

在该活动的清单文件中。

我需要为用户展示广告。如果我销毁它,用户将无法看到广告。 - Artem
当您不想要时,只需将其可见性设置为GONE,然后在您想显示广告时将其设置为VISIBLE。 - Ashish Shukla
是的,但我不想在键盘打开时显示广告。这是请求监听键盘打开/关闭。 - Artem

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接