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 で新しいアクティビティを開始していることです。新しいアクティビティを開始せず、現在のアクティビティを再利用してください。

この場合、次のようなものです。

@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 に答える