1

在我的 Android 测验应用中,我实现了 Admob。但是每次给出答案并且问题发生变化时,Admob 也会刷新,这大约需要 3-4 秒。但到那时,问题再次发生变化,依此类推。

QuestionActivity.class:

package com.tsc.walkingdead;

import java.util.List;
import com.google.ads.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;



import com.tsc.walkingdead.R;
import com.tsc.walkingdead.quiz.GamePlay;
import com.tsc.walkingdead.quiz.Question;
import com.tsc.walkingdead.util.Utility;


public class QuestionActivity extends Activity implements OnClickListener{

    private Question currentQ;
    private GamePlay currentGame;
;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);

// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());

        /**
         * Configure current game and get question
         */
        currentGame = ((WalkingDeadApplication)getApplication()).getCurrentGame();
        currentQ = currentGame.getNextQuestion();
        Button nextBtn = (Button) findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener(this);

        /**
         * Update the question and answer options..
         */
        setQuestions();

    }


    /**
     * Method to set the text for the question and answers from the current games
     * current question
     */
    private void setQuestions() {
        //set the question text from current question
        String question = Utility.capitalise(currentQ.getQuestion()) + "??";
        TextView qText = (TextView) findViewById(R.id.question);
        qText.setText(question);

        //set the available options
        List<String> answers = currentQ.getQuestionOptions();
        TextView option1 = (TextView) findViewById(R.id.answer1);
        option1.setText(Utility.capitalise(answers.get(0)));

        TextView option2 = (TextView) findViewById(R.id.answer2);
        option2.setText(Utility.capitalise(answers.get(1)));

        TextView option3 = (TextView) findViewById(R.id.answer3);
        option3.setText(Utility.capitalise(answers.get(2)));

        TextView option4 = (TextView) findViewById(R.id.answer4);
        option4.setText(Utility.capitalise(answers.get(3)));
    }


    @Override
    public void onClick(View arg0) {
        //Log.d("Questions", "Moving to next question");

        /**
         * validate a checkbox has been selected
         */
        if (!checkAnswer()) return;


        /**
         * check if end of game
         */
        if (currentGame.isGameOver()){
            Intent i = new Intent(this, EndgameActivity.class);
            startActivity(i);
            finish();
        }
        else{
            Intent i = new Intent(this, QuestionActivity.class);
            startActivity(i);
            finish();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
        case KeyEvent.KEYCODE_BACK :
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


    /**
     * Check if a checkbox has been selected, and if it
     * has then check if its correct and update gamescore
     */
    private boolean checkAnswer() {
        String answer = getSelectedAnswer();
        if (answer==null){
            //Log.d("Questions", "No Checkbox selection made - returning");
            return false;
        }
        else {
            //Log.d("Questions", "Valid Checkbox selection made - check if correct");
            if (currentQ.getAnswer().equalsIgnoreCase(answer))
            {
                //Log.d("Questions", "Correct Answer!");
                currentGame.incrementRightAnswers();
                Toast.makeText(this, "Correct!", Toast.LENGTH_LONG).show();
            }

            else{
                //Log.d("Questions", "Incorrect Answer!");
                currentGame.incrementWrongAnswers();
                Toast.makeText(this, "Wrong!", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    }


    /**
     * 
     */
    private String getSelectedAnswer() {
        RadioButton c1 = (RadioButton)findViewById(R.id.answer1);
        RadioButton c2 = (RadioButton)findViewById(R.id.answer2);
        RadioButton c3 = (RadioButton)findViewById(R.id.answer3);
        RadioButton c4 = (RadioButton)findViewById(R.id.answer4);
        if (c1.isChecked())
        {
            return c1.getText().toString();
        }
        if (c2.isChecked())
        {
            return c2.getText().toString();
        }
        if (c3.isChecked())
        {
            return c3.getText().toString();
        }
        if (c4.isChecked())
        {
            return c4.getText().toString();
        }

        return null;
    }

}

问题.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/walking_dead_background2"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:gravity="center"
    android:orientation="vertical" 
    >

<LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center">

        <RadioGroup
            android:id="@+id/group1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/question"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                style="@style/myStyle1"
                 android:layout_marginBottom="10dp"/>

            <RadioButton
                android:id="@+id/answer1"
                android:checked="false" 
                style="@style/myStyle"/>

            <RadioButton
                android:id="@+id/answer2"
                android:checked="false" 
                style="@style/myStyle"/>

            <RadioButton
                android:id="@+id/answer3"
                android:checked="false" 
                style="@style/myStyle"/>

            <RadioButton
                android:id="@+id/answer4"
                android:checked="false" 
                style="@style/myStyle"/>

                    <Button
                        android:id="@+id/nextBtn"
                        style="@style/myStyle"
                        android:layout_width="80dip"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:background="@drawable/button_selector"
                        android:paddingBottom="5dip"
                        android:paddingTop="5dip"
                        android:text="Next"
                        android:textColor="#ffffff" />

        </RadioGroup>

</LinearLayout>
<LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:id="@+id/mainLayout">
    <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         ads:adUnitId="ca-app-pub-6195077402939893/2503837566"
                         ads:adSize="BANNER"
                         ads:testDevices="TEST_EMULATOR"/>




</LinearLayout>
</LinearLayout>

是否有可能让广告一直显示,尽管问题正在改变?

谢谢

4

2 回答 2

0

尝试删除

  adView.loadAd(new AdRequest());

从您的 onCreate 并添加

ads:loadAdOnCreate="true"

到您的广告 XML。

于 2013-09-26T19:17:33.340 回答
0

您的问题是您正在#onClick 中启动一个新活动。不要启动新的Activity,重用当前的Activity。

在这种情况下,类似于:

@Override
public void onClick(View arg0) {
    //Log.d("Questions", "Moving to next question");

    /**
     * validate a checkbox has been selected
     */
    if (!checkAnswer()) return;


    /**
     * check if end of game
     */
    if (currentGame.isGameOver()){
        Intent i = new Intent(this, EndgameActivity.class);
        startActivity(i);
        finish();
    }
    else{
        currentQ = currentGame.getNextQuestion(); 
        setQuestions();
    }
}
于 2013-09-28T08:54:27.450 回答