2015-09-10 2 views
3

J'utilise MoPubView pour les annonces dans mon application. Mais quand j'ouvre le clavier, cette bannière se lève et ferme les widgets.Comment masquer bannière publicitaire lorsque clavier ouvert sur Android

enter image description here

Ceci est du code de mise en page:

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

Ce schéma de cette disposition: enter image description here

Comment cacher cette bannière ou le fixer au fond?

+0

Je pense que cette réponse peut vous aider. vérifier: http://stackoverflow.com/a/26176575/2943501 –

+0

Cela semble être la solution la plus élégante: http://stackoverflow.com/a/26176575/438466 – Rabi

Répondre

2

Créer une classe pour gérer la détection du clavier,

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(); 
      } 
     } 
    } 
} 

Dans la méthode onCreate de votre AdActivity, insérer ces lignes:

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

@ArtemShevchenko ....! Si vous avez une question, vous pouvez me demander ... :) –

+0

@ Asal Imam - Salut l'homme, merci, mais - http://joxi.ru/ZrJVlnVh4jwkrj - quel ID dois-je définir ici? Si j'ai beaucoup de widgets (EditText) pour l'entrée - son besoin d'un keyboardlistener pour chaque edittext? – Artem

+1

Déclarez un ID de votre mise en page principale (pas EditText) en XML ..... Cela fonctionnera bien –

1

Utilisez le code suivant lorsque vous ne voulez pas montrer l'annonce

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

Edit: Essayez usi ng ceci:

android:windowSoftInputMode="adjustPan"; 

dans votre Manifest pour cette activité

+0

Moi besoin d'afficher l'annonce pour les utilisateurs. il - les utilisateurs ne peuvent pas voir l'annonce ( – Artem

+0

vous pouvez simplement définir sa visibilité à GONE lorsque vous ne voulez pas, puis mettre à VISIBLE lorsque vous souhaitez afficher votre annonce –

+0

Oui, mais je ne veux pas afficher l'annonce lorsque le clavier a été ouvert. écoutez le clavier ouvert/fermé. – Artem

0

résolu ce problème en ajoutant

android: windowSoftInputMode = "adjustPan"

dans AndroidManifest.xml

<activity 
android:name=".MainActivity" 
android:label="@string/app_name" 
android:windowSoftInputMode="adjustPan" 
android:theme="@style/AppTheme.NoActionBar"> 
</activity>