我爲兒子開發的第一款Android App,用於九九乘法練習

用一天時間在macbook上安裝好了Android Studio For Mac,注意dl.google.com只支持電信網絡下載,家裏寬帶如果是移動或者聯通的,使用AS下載Android SDK和後面新建project下載gradle時網絡都有問題(移動寬帶無法直接下載,需要設置代理proxy)

今天用不到一天時間,爲兒子開發的第一款Android App,用於九九乘法練習

github下載地址:https://github.com/sinodragon21/MultiplicationTable.git

package com.nathan.multiplicationtable;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    int factor = 1, factorBase = 9;
    int faciend = 1, faciendBase = 9;

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

        refreshQuestion();
    }

    public void calculateMT(View view){
        EditText editTextAnswer = findViewById(R.id.editTextAnswer);
        TextView textViewHints = findViewById(R.id.textViewHints);
        int answer = Integer.parseInt(String.valueOf(editTextAnswer.getText()));
        if(answer == factor * faciend) {
            textViewHints.setText(R.string.right);
        }else{
            String strHints = String.format("%d x %d = %d", factor, faciend, factor*faciend);
            textViewHints.setText(this.getString(R.string.wrong)+"\n"+strHints);
        }
    }

    public void nextQuestion(View view){
        refreshQuestion();
    }

    void refreshQuestion(){
        Random rand = new Random();
        factor = rand.nextInt(factorBase) + 1;
        faciend = rand.nextInt(faciendBase) + 1;
        // String question = Integer.toString(factor) + "x" + Integer.toString(faciend) + "=";
        TextView textViewA = findViewById(R.id.textViewA);
        TextView textViewB = findViewById(R.id.textViewB);
        EditText editTextAnswer = findViewById(R.id.editTextAnswer);
        TextView textViewHints = findViewById(R.id.textViewHints);
        textViewA.setText(Integer.toString(factor));
        textViewB.setText(Integer.toString(faciend));
        editTextAnswer.setText("");
        textViewHints.setText("");
    }
}

APP的截圖如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章