【问题标题】:Remove ad banner completely when button click单击按钮时完全删除广告横幅
【发布时间】:2018-07-16 08:02:40
【问题描述】:

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

public class MainActivity extends AppCompatActivity {
AdView Adview;
TextView textView6;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

}
public void onClickValuation(View view) {
    Intent intent= new Intent(this,ValuationActivity.class);
    startActivity(intent);
}
public void onClickItem(View view) {
    Intent intent= new Intent(this,ItemActivity.class);
    startActivity(intent);
}
public void onClickAds(View view){
    ViewGroup parent1 = (ViewGroup) Adview.getParent();
    ViewGroup parent2 = (ViewGroup) textView6.getParent();
    parent1.removeView(Adview);
    parent2.removeView(textView6);
    parent1.invalidate();
    parent2.invalidate();
}
}

XML

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

【问题讨论】:

    标签: android textview admob banner


    【解决方案1】:

    编写持久性逻辑(数据库/文件/共享首选项)并检查用户之前是否点击过,然后只加载广告。对于数据和文件存储,请参阅此链接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
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2019-07-11
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多