仅在加载插页式广告时显示按钮,否则显示TextView?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仅在加载插页式广告时显示按钮,否则显示TextView?相关的知识,希望对你有一定的参考价值。
我正在尝试显示一个显示AdMob插页式广告的按钮,并在关闭时将用户带到下一个活动。但是我希望在加载插页式广告时可以看到按钮,直到那时我想显示一个说“应用正在加载...”的TextView。
这是我到目前为止创造的。
Button goFree;
private TextView loadingFree;
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask);
Button goFree = (Button) findViewById(R.id.goFree);
loadingFree = findViewById(R.id.loadingFeee);
MobileAds.initialize(RegisterNote.this, "cca-app-pub-3940256099942544/1033173712");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
startActivity(new Intent(Ask.this, Free.class));
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
if (mInterstitialAd.isLoaded()) {
goFree.setVisibility(View.VISIBLE);
lodingFree.setVisibility(View.GONE);
} else {
goFree.setVisibility(View.GONE);
lodingFree.setVisibility(View.VISIBLE);
}
goFree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mInterstitialAd.show();
}
});
}
但问题是,即使广告加载,TextView可见性仍保持VISIBLE
,按钮保持GONE
。也许是因为广告加载时,它不会调用函数来更改可见性。
我该怎么办?如何在加载广告后才能显示按钮?
答案
尝试像使用onAdLoaded()
一样使用onAdClosed()
function,代码应类似于:
goFree.setVisibility(View.GONE);
lodingFree.setVisibility(View.VISIBLE);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
goFree.setVisibility(View.VISIBLE);
lodingFree.setVisibility(View.GONE);
}
@Override
public void onAdClosed() {
startActivity(new Intent(Ask.this, Free.class));
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
然后你可以删除检查if else
。
资料来源:https://developers.google.com/android/reference/com/google/android/gms/ads/InterstitialAd
希望这可以帮助
另一答案
AdListener中的每个可覆盖方法都对应于广告生命周期中的事件。
您可以在加载add时在onAdLoaded()方法中编写代码,它将隐藏textview和visible按钮。
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
goFree.setVisibility(View.VISIBLE);
lodingFree.setVisibility(View.GONE);
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
// Code to be executed when when the interstitial ad is closed.
}
另一答案
Check the below example that i have implemented. Hope it will help you. Just check this method
holder.cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// showInterstitial();
if (interstitialAd != null && interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
Intent intent = new Intent();
Bundle bundle =
ActivityOptions.makeSceneTransitionAnimation((Activity) mContext,
holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
intent.setClass(mContext, AboutMovieActivity.class);
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID,
mSearchedMovies.get(position).getId());
intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH,
mSearchedMovies.get(position).getPosterPath());
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME,
mSearchedMovies.get(position).getTitle());
mContext.startActivity(intent, bundle);
startGame();
}
interstitialAd.setAdListener(new AdListener(){
@Override
public void onAdClosed() {
Intent intent = new Intent();
Bundle bundle =
ActivityOptions.makeSceneTransitionAnimation((Activity) mContext,
holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
intent.setClass(mContext, AboutMovieActivity.class);
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID,
mSearchedMovies.get(position).getId());
intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH,
mSearchedMovies.get(position).getPosterPath());
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME,
mSearchedMovies.get(position).getTitle());
mContext.startActivity(intent, bundle);
}
});
Full code is below:-
public class MoviesSearchAdapter extends
RecyclerView.Adapter<MoviesSearchAdapter.ViewHolder> {
Context mContext;
private ArrayList<Movie> mSearchedMovies;
private InterstitialAd interstitialAd;
public MoviesSearchAdapter(ArrayList<Movie> searchedMovies, Context context) {
mContext = context;
mSearchedMovies = searchedMovies;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(parent.getContext()).inflate
(R.layout.search_movie_list_item,
parent, false);
// Initialize the Mobile Ads SDK. ca-app-pub-7152815429504851~7360705844
MobileAds.initialize(mContext, "ca-app-pub-7152815429504851~8085885141");
// Create the InterstitialAd and set the adUnitId.
interstitialAd = new InterstitialAd(mContext);
// Defined in res/values/strings.xml ca-app-pub-
7152815429504851/2822695030
interstitialAd.setAdUnitId("ca-app-pub-7152815429504851/4913761040");
startGame();
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (mSearchedMovies != null) {
holder.name.setText(mSearchedMovies.get(position).getTitle());
Picasso.with(mContext).load(URLConstants.IMAGE_BASE_URL +
mSearchedMovies.get(position).getPosterPath()).into(holder.thumbnailImage);
if (mSearchedMovies.get(position).getDate().length() >= 5) {
String date = mSearchedMovies.get(position).getDate().substring(0,
4);
holder.releaseDate.setText(date);
}
String rating =
Double.toString(mSearchedMovies.get(position).getRating());
holder.rating.setText(rating);
holder.cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// showInterstitial();
if (interstitialAd != null && interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
Intent intent = new Intent();
Bundle bundle =
ActivityOptions.makeSceneTransitionAnimation((Activity) mContext,
holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
intent.setClass(mContext, AboutMovieActivity.class);
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID,
mSearchedMovies.get(position).getId());
intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH,
mSearchedMovies.get(position).getPosterPath());
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME,
mSearchedMovies.get(position).getTitle());
mContext.startActivity(intent, bundle);
startGame();
}
interstitialAd.setAdListener(new AdListener(){
@Override
public void onAdClosed() {
Intent intent = new Intent();
Bundle bundle =
ActivityOptions.makeSceneTransitionAnimation((Activity) mContext,
holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
intent.setClass(mContext, AboutMovieActivity.class);
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID,
mSearchedMovies.get(position).getId());
intent.putExtra(IntentConsta以上是关于仅在加载插页式广告时显示按钮,否则显示TextView?的主要内容,如果未能解决你的问题,请参考以下文章
在每 x 次加载的 viewdidload 上显示插页式广告