小编典典

两个点击计数器相互交互

all

我有 5 个按钮和 2 个菜单按钮:

  1. First list

  2. back,

  3. random,
  4. next

  5. Second list

  6. copy,

  7. share,
  8. first quote,
  9. latest quote

菜单按钮是第一个报价和最新报价

在第一个按钮列表中,我添加了一个 10 的计数器,这意味着点击 10 次后应该显示插页式广告。

在第二个按钮列表中,我添加了一个计数器 3,这意味着点击 3 次后应该显示插页式广告。

现在问题来了,当第一个按钮计数器达到 9(意味着单击后它应该显示广告)并且我按下 2 中的任何按钮 -> 广告正在显示

MainActivity.java

int triggerClicks = 3; // for copy,share,firstquote & latestquote
int countClicks = 0;
int triggerClicksNav = 10;// for back,next & random

@SuppressLint("SetTextI18n")
private void latestQuote() {
    countClicks++;
    if (countClicks != 3) {
        position = quotes_list.size() - 1;
        quotesTxt.setText(quotes_list.get(position));
        countTxt.setText(position + "/" + quotes_list.size());
        Log.d(TAG, quotes_list.toString());
    }
    if (mInterstitialAd != null && countClicks >= triggerClicks) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                position = quotes_list.size() - 1;
                quotesTxt.setText(quotes_list.get(position));
                countTxt.setText(position + "/" + quotes_list.size());
                Log.d(TAG, quotes_list.toString());
                countClicks = 0;
            }
        });
    }
}

@SuppressLint("SetTextI18n")
private void firstQuote() {
    countClicks++;
    if (countClicks != 3) {
        position = 0;
        quotesTxt.setText(quotes_list.get(position));
        countTxt.setText(position + "/" + quotes_list.size());
        Log.d(TAG, quotes_list.toString());
    }
    if (mInterstitialAd != null && countClicks >= triggerClicks) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                position = 0;
                quotesTxt.setText(quotes_list.get(position));
                countTxt.setText(position + "/" + quotes_list.size());
                countClicks = 0;
            }
        });
    }
}


@SuppressLint("SetTextI18n")
private void back() {
    countClicks++;
    if (position > 0) {
        position = (position - 1) % quotes_list.size();
        quotesTxt.setText(quotes_list.get(position));
        countTxt.setText(position + "/" + quotes_list.size());
        Log.d(TAG, quotes_list.toString());
    }
    if (mInterstitialAd != null && countClicks >= triggerClicksNav) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                countClicks = 0;
            }
        });
    }
}

@SuppressLint("SetTextI18n")
private void next() {
    countClicks++;
    position = (position + 1) % quotes_list.size();
    quotesTxt.setText(quotes_list.get(position));
    countTxt.setText(position + "/" + quotes_list.size());
    if (mInterstitialAd != null && countClicks >= triggerClicksNav) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                countClicks = 0;
            }
        });
    }
}

@SuppressLint("SetTextI18n")
private void random() {
    countClicks++;
    position = randomQ.nextInt(quotes_list.size());
    quotesTxt.setText(quotes_list.get(position));
    countTxt.setText(position + "/" + quotes_list.size());
    if (mInterstitialAd != null && countClicks >= triggerClicksNav) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                countClicks = 0;
            }
        });
    }
}


private void copy() {
    countClicks++;
    ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clipData = ClipData.newPlainText("text", quotesTxt.getText());
    if (clipboardManager != null) {
        clipboardManager.setPrimaryClip(clipData);
    }
    Toast.makeText(getApplicationContext(), "Copied", Toast.LENGTH_SHORT).show();
    if (mInterstitialAd != null && countClicks >= triggerClicks) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                countClicks = 0;
            }
        });
    }
}

private void share() {
    countClicks++;
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, quotesTxt.getText());
    startActivity(Intent.createChooser(intent, "Share to"));
    if (mInterstitialAd != null && countClicks >= triggerClicks) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                countClicks = 0;
            }
        });
    }
 }

如果我为每个按钮分配一个不同的计数器,问题就会得到解决,但我不喜欢这个解决方案,如果你有其他解决方案,请添加它

int countClicksBack = 0;
int countClicksRandom = 0;
int countClicksNext = 0;
int countClicksCopy = 0;
int countClicksShare = 0;
int countClicksFirst = 0;
int countClicksLatest = 0;

阅读 83

收藏
2022-06-30

共1个答案

小编典典

问题是您使用相同的计数器来显示您的广告,但是,如果我理解,您需要 2 个不同的计数器:一个用于按钮,一个用于菜单。正确的 ?因此,您必须声明 2 个不同的计数器。

顺便说一句,您的代码不是“干净的”:用于展示广告的代码可以以自己的方法导出,如下所示:

private void showAd() {
   mInterstitialAd.show(HomeActivity.this);
   mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
      @Override
      public void onAdDismissedFullScreenContent() {
         super.onAdDismissedFullScreenContent();
         mInterstitialAd = null;
         intertitalAd();
         countClicks = 0;
      }
   });
}

private void back() {
    // your stuff
    if (mInterstitialAd != null && countClicks >= triggerClicksNav) {
        showAd();
    }
}
2022-06-30