单击按钮时完全删除广告横幅

时间:2018-07-16 08:02:41

标签: android textview admob banner

我在应用程序上放置了一个广告横幅,并希望在用户点击文字时将其和文字完全删除。但是,当我关闭并重新启动应用程序时,以下代码将起作用,横幅和文本再次出现。 我应该设置可见性消失吗?如果我这样做,那不是说广告横幅仍然存在但是看不到(违反AdMob TnC吗?)。谢谢

enter image description here

<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"

    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
    ads:layout_constraintBottom_toBottomOf="parent"
    ads:layout_constraintEnd_toEndOf="parent"
    ads:layout_constraintStart_toStartOf="parent"
    ads:layout_constraintTop_toBottomOf="@+id/constraintLayout"
    ads:layout_constraintVertical_bias="0.42"></com.google.android.gms.ads.AdView>

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onClickAds"
    android:text="Don't like seeing ads? Tap Here!"
    android:textColor="@android:color/background_light"
    android:textStyle="bold|italic"
    app:layout_constraintBottom_toTopOf="@+id/adView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

XML

10000

1 个答案:

答案 0 :(得分:0)

编写持久性逻辑(数据库/文件/共享首选项),并检查用户以前是否单击过,然后仅加载广告。对于数据和文件存储,请参考此链接https://developer.android.com/guide/topics/data/data-storage

示例代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView6=findViewById(R.id.textView6);

    if(!IsUserRegistered()){
    MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713");
    Adview=findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()/*.addTestDevice("")*/.build();
    Adview.loadAd(adRequest);
    }

}

public void onClickAds(View view){
    ViewGroup parent1 = (ViewGroup) Adview.getParent();
    ViewGroup parent2 = (ViewGroup) textView6.getParent();
    parent1.removeView(Adview);
    //Ad is removed now update UserRegistered
    UpdateUserRegistered(true);
    parent2.removeView(textView6);
    parent1.invalidate();
    parent2.invalidate();
}   

private boolean IsUserRegistered(){
//User Data Persistance to check if value is set and return that value
//https://developer.android.com/guide/topics/data/data-storage
    return false;

}

private void UpdateUserRegistered(Boolean bUserRegister)
{
//User Data Persistance to update user send value
    //https://developer.android.com/guide/topics/data/data-storage

}