如何通过声音做出多重选择

时间:2019-09-16 15:51:44

标签: java android-studio

我需要在测验应用程序上完成选择多项任务,链接到声音和图像,然后才能完成任务。 我有20个问题(6个音频问题,6个图片问题和8个单词问题)。 1.用户打开活动按下播放按钮,然后听音频文件并选择答案(对还是错),然后出现下一个问题,直到完成第6个问题,然后显示分数。 2.用户打开显示图片的活动,然后用户选择答案(对还是错),然后出现下一个问题,直到第6个问题完成,然后显示分数。 3.用户打开单词出现的活动,然后用户选择答案图片(对还是错),然后出现下一个问题,直到完成8个问题,然后显示分数。 我不知道如何使用音频和图片,而且我的随机代码也存在一些问题。任何帮助,将不胜感激。

我主要活动中的代码

public class MainActivity extends AppCompatActivity {

    Button answer1, answer2, answer3, answer4;

    TextView score, question;

    private Questions mQuestions = new Questions();

    private String mAnswer;
    private int mScore = 0;
    private int mQuestionsLenght = mQuestions.mQuestions.length;

    Random r;

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

        r = new Random();

        answer1 = findViewById(R.id.answer1);
        answer2 = findViewById(R.id.answer2);
        answer3 = findViewById(R.id.answer3);
        answer4 = findViewById(R.id.answer4);

        score = findViewById(R.id.score);
        question = findViewById(R.id.question);

        score.setText("Score: " + mScore);
        updateQuestion(r.nextInt(mQuestionsLenght));

        answer1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(answer1.getText() == mAnswer){
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLenght));
                } else {
                    gameOver();
                }
            }
        });

        answer2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(answer2.getText() == mAnswer){
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLenght));
                } else {
                    gameOver();
                }
            }
        });

        answer3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(answer3.getText() == mAnswer){
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLenght));
                } else {
                    gameOver();
                }
            }
        });

        answer4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(answer4.getText() == mAnswer){
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLenght));
                } else {
                    gameOver();
                }
            }
        });
    }

    private void updateQuestion(int num){
        question.setText(mQuestions.getQuestion(num));
        answer1.setText(mQuestions.getChoice1(num));
        answer2.setText(mQuestions.getChoice2(num));
        answer3.setText(mQuestions.getChoice3(num));
        answer4.setText(mQuestions.getChoice4(num));

        mAnswer = mQuestions.getCorrectAnswer(num);
    }

    private void gameOver(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
            alertDialogBuilder
                    .setMessage("Game Over! Your score is" + mScore + "points.")
                    .setCancelable(false)
                    .setPositiveButton("New Game",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    startActivity(new Intent(getApplicationContext(),MainActivity.class));
                                    finish();
                                }
                            })

                    .setNegativeButton("Exit",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    finish();
                                }
                            });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
    }
}

(只是我的Questions.java中的示例问题)

 public String mQuestions[] = {
            "What color is it?",
            "What color do you see?",
            "What color do you want?",
            "What color do you like?",
            "What is it?",
            "What is this?",
            "What is that?",
            "What are these?",
            "What are those?"
    };

    private String mChoices[][] = {
            {"blue", "yellow", "orange", "red"},
            {"pink", "purple", "black", "white"},
            {"gray", "gold", "silver", "tan"},
            {"violet", "light green", "deep blue", "khaki"},
            {"a cat", "a dog", "a bird", "a fish"},
            {"a book", "a pen", "a pencil", "a ruler"},
            {"a car", "a bike", "a table", "a chair"},
            {"apples", "bananas", "grapes", "cherries"},
            {"carrots", "cucumbers", "turnips", "onions"}
    };

    private String mCorrectAnswers[] = {"yellow", "white", "silver", "violet", "a bird", "a ruler", "a bike", "grapes", "carrots"};

    public String getQuestion(int a){
        String question = mQuestions[a];
        return question;
    }

    public String getChoice1(int a){
        String choice = mChoices[a][0];
        return choice;
    }

    public String getChoice2(int a){
        String choice = mChoices[a][1];
        return choice;
    }

    public String getChoice3(int a){
        String choice = mChoices[a][2];
        return choice;
    }

    public String getChoice4(int a){
        String choice = mChoices[a][3];
        return choice;
    }

    public String getCorrectAnswer (int a){
        String answer = mCorrectAnswers[a];
        return answer;
    }
}

0 个答案:

没有答案