android开发---简单答题器(JAVA)

发布时间:2024年01月24日

根据《android编程权威指南》一书的前六章 写的代码

首先MainActivity.java

package com.example.test01;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button trueButton;
    private Button falseButton;

    private Button nextButton;
    private Button pirrButton;
    private TextView questionTextView;

    private String TAG="MainActivity";

    public static final String EXTRA_PARAM="extre_param";
    private List<Question> questionBank = new ArrayList<>();
    private int currentIndex = 0;
    private Button cheatButton;

    private int REQUEST=0;

    private void updateQuestion() {

        Question[] array = questionBank.toArray(new Question[currentIndex]);
        int questionTextResId = array[currentIndex].textResId;

        questionTextView.setText(questionTextResId);

    }

    private void checkAnswer(boolean userAnswer) {
        Question[] array = questionBank.toArray(new Question[currentIndex]);
         boolean correctAnswer = array[currentIndex].answer;

        if (userAnswer == correctAnswer)
            Toast.makeText(this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
        else Toast.makeText(this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
    }




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


        Log.d(TAG,"onCreate(Bundle?)called");
        trueButton = findViewById(R.id.true_button);
        falseButton = findViewById(R.id.false_button);
        nextButton = findViewById(R.id.next_button);
        pirrButton = findViewById(R.id.pir_button);
        cheatButton = findViewById(R.id.cheat_button);

        questionTextView = findViewById(R.id.question_text_view);

        questionBank.add(new Question(R.string.question_australia, true));
        questionBank.add(new Question(R.string.question_asia, false));
        questionBank.add(new Question(R.string.question_africa, true));
        questionBank.add(new Question(R.string.question_oceans, false));

        trueButton.setOnClickListener(new View.OnClickListener() {
             @Override
            public void onClick(View v) {

                 checkAnswer(true);
            }

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

                checkAnswer(false);
            }
        });
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = (currentIndex + 1) % questionBank.size();
                if(currentIndex<1)currentIndex=1;

                updateQuestion();

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

                Intent intent = new Intent();
                intent.setClass(MainActivity.this, CheatActivity.class);
                Question[] array = questionBank.toArray(new Question[currentIndex]);
                boolean correctAnswer = array[currentIndex].answer;

                intent.putExtra(EXTRA_PARAM,correctAnswer);

                startActivity(intent);
            }
        });
        pirrButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              currentIndex = (currentIndex - 1) %  questionBank.size();
                if(currentIndex<1)currentIndex=1;
                updateQuestion();

            }
        });
        updateQuestion();


    }
    protected void onStart(){
        super.onStart();
        Log.d(TAG,"onstart() called");
    }
}

CheatActivity.java

package com.example.test01;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CheatActivity extends AppCompatActivity {

    public  String EXTRA_ANSWER_IS_TRUE="com.bignerdranch.android.geoquiz.answer_is_true";
    String EXTRA="com.bignerdranch.android.geoquiz.answer_is_true";
    private boolean answerIsTrue=false;
    private Intent intent;
    private Button showAnswerButton;
    private TextView answerTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        intent=new Intent();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);
        answerIsTrue = intent.getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
        showAnswerButton=findViewById(R.id.show_answer_button);
        answerTextView=findViewById(R.id.answer_text_view);
        showAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent data=getIntent();
                boolean a=data.getBooleanExtra(MainActivity.EXTRA_PARAM,false);

                if (a){
                    answerTextView.setText("ture");
                }
                else answerTextView.setText("false");
            }
        });

    }

}

Question.java

package com.example.test01;

//public class Question {
//}

import androidx.annotation.StringRes;
class Question{
    @StringRes
    int textResId;
    boolean answer;
    public Question(int textResId,boolean answer){
        this.textResId=textResId;
        this.answer=answer;
    }
}

自己找一个next.png

activity_cheat.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".CheatActivity">
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:text="@string/warning_text"/>
    <TextView
        android:id="@+id/answer_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="Answer"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/show_answer_button"
        android:text="@string/show_answer_button"/>

</LinearLayout >

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/question_australia" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button" />
        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableEnd="@drawable/next"
            android:drawablePadding="4dp"
            android:text="@string/next_button" />

        <Button
            android:id="@+id/pir_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableEnd="@drawable/next"
            android:drawablePadding="4dp"
            android:text="@string/pir_button" />

        <Button
            android:id="@+id/cheat_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"            android:drawableEnd="@drawable/next"
            android:drawablePadding="24dp"
            android:text="cheat" />

    </LinearLayout>

</LinearLayout>

AndroidManifest.xml(我这个project是test01)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Test01"

        tools:targetApi="31">
        <activity android:name=".CheatActivity"/>
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

android开发慢慢长路,去日苦多,加油吧

文章来源:https://blog.csdn.net/2202_75787160/article/details/135825742
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。