从活动上下文访问对话框按钮

时间:2018-11-28 23:37:20

标签: java android-studio dialog admob-rewardedvideoad

我正在尝试在包含游戏的应用中实施Google AdMob奖励视频。当玩家失败时,将为他显示一个代表“游戏结束”屏幕的对话框,其中包含两个按钮:一个按钮重新启动游戏,另一个按钮使他有机会通过观看奖励视频从他离开的地方继续播放。为此,我在游戏活动中创建了mRewardedVideo对象及其侦听器,并在名为Game的类中创建了showLoseDialog函数。当玩家观看完视频后,我想隐藏重新启动游戏的按钮,并更改按钮的文本,指示它已经可以收到奖励了,以防万一它可以从停止的地方继续播放。问题在于,当用户结束观看视频时调用的Listener的onRewardedVideoCompleted方法位于Activity中,而按钮位于Game类的函数中,也就是说,我不知道如何从菜单中访问按钮。侦听器方法。有人知道这是否可能吗?

MainActivity.java:

public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    mRewardedVideoAd.setRewardedVideoAdListener(this);

    loadRewardedVideoAd();

}

@Override
public void onResume() {
    mRewardedVideoAd.resume(this);
    super.onResume();
}

@Override
public void onPause() {
    mRewardedVideoAd.pause(this);
    super.onPause();
}

@Override
public void onDestroy() {
    mRewardedVideoAd.destroy(this);
    super.onDestroy();
}

@Override
public void onRewardedVideoAdClosed() {
    loadRewardedVideoAd();
}

@Override
public void onRewarded(RewardItem rewardItem) {
    Toast.makeText(this, "onRewarded! currency: " + rewardItem.getType() + "  amount: " +
            rewardItem.getAmount(), Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {

}

@Override
public void onRewardedVideoCompleted() {
    //btnPlayAgain.setVisibility(View.GONE);
    //btnRewardedVideo.setText(R.string.receiveReward);
}

public void lose(){
    Game.showLoseDialog(MainActivity.this, getLayoutInflater().inflate(R.layout.dialog_lose, null), mRewardedVideoAd);
}

private void loadRewardedVideoAd() {
    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            new AdRequest.Builder().build());
}

Game.java:

public class Game {
public static void showLoseDialog(Context context, View dialogView, final RewardedVideoAd mRewardedVideoAd){
    Button btnPlayAgain = dialogView.findViewById(R.id.btnPlayAgain);

    AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);

    mBuilder.setView(dialogView);
    final AlertDialog dialog = mBuilder.create();

    final Button btnRewardedVideo = dialogView.findViewById(R.id.btnRewardedVideo);
    btnRewardedVideo.setVisibility(View.VISIBLE);

    if(mRewardedVideoAd.isLoaded()){
        btnRewardedVideo.setVisibility(View.VISIBLE);
    }else{
        btnRewardedVideo.setVisibility(View.GONE);
    }

    btnPlayAgain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    btnRewardedVideo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mRewardedVideoAd.isLoaded()) {
                mRewardedVideoAd.show();
            }
        }
    });

    dialog.show();
}

0 个答案:

没有答案