Android addition and subtraction app within 100 (1)

insert image description here
t1t1t1t1

written in front

It’s just that I haven’t written an app independently before, and this one doesn’t use any framework. It’s purely for children at home to play with.

Function

After entering the app, there is an interface with two question marks.
Click to start
. There is a problem. In the background, there is multiplication and division, but on the one hand, the child has not learned multiplication and division
.
If you make a mistake, there will be a prompt and vibration

Some functions or bugs to be fixed

1. Continuously clicking the button will flash back
2. I haven't figured out what to do with the end button
3. The data is not persisted
4. The interface is too ugly...

the code

  //声明控件
    private Button next;
    private Button exit;

    private TextView number1;
    private TextView number2;
    private TextView guanxi1;
    private TextView top;

    private EditText an;
    private String fail = "计算错误!";
    private String ok = "计算正确!";


    private int t1 = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        //隐藏标题栏
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);

        //找到控件
        next = findViewById(R.id.next);
        exit = findViewById(R.id.exit);

        next.setOnClickListener(this::onClick);
        exit.setOnClickListener(this::onClick2);

        //建立对象
        number1 = findViewById(R.id.number1);
        number2 = findViewById(R.id.number2);
        guanxi1 = findViewById(R.id.guanxi);
        an = findViewById(R.id.an);
        top = findViewById(R.id.topnum);

    }


    //开始按钮
    public void onClick(View view) {
    
    
        int math = 0;
        //获取目前数字 关系 答案
        String m1 = number1.getText().toString();//获取首位数字
        if (!isNumeric(m1)) {
    
    
            int n0;
            String n1 = "";
            String n2 = "";

            int g = (int) (Math.random() * 2);
            if (g == 0) {
    
    
                guanxi1.setText("+");
                n0 = (int) (Math.random() * 100);
                n1 = String.valueOf(n0);
                n2 = String.valueOf((int) (10 + Math.random() * (90 - n0)));
            } else if (g == 1) {
    
    
                guanxi1.setText("-");
                n0 = (int) (10 + Math.random() * 90);
                n1 = String.valueOf(n0);
                n2 = String.valueOf((int) (10 + Math.random() * n0));
            } else if (g == 2) {
    
    
                guanxi1.setText("*");
            } else if (g == 3) {
    
    
                guanxi1.setText("/");
            }
            number1.setText(n1);
            number2.setText(n2);
            next.setText("下一道");

        } else {
    
    
            int n0;
            String n1 = "";
            String n2 = "";
            int num1 = Integer.parseInt(number1.getText().toString());//获取首位数字
            int num2 = Integer.parseInt(number2.getText().toString());//获取次位数字
            String gunxi = guanxi1.getText().toString();//获取关系
            int a1 = Integer.parseInt(an.getText().toString());//获取用户答案
            int t1 = Integer.parseInt(top.getText().toString());//获取top
            math = Math(num1, gunxi, num2);//系统计算

            if (math == a1) {
    
    
                ToastUtil.showMsg(getApplicationContext(), ok);
                String t = String.valueOf(1 + t1);

                int g = (int) (Math.random() * 2);
                if (g == 0) {
    
    
                    guanxi1.setText("+");
                    n0 = (int) (Math.random() * 100);
                    n1 = String.valueOf(n0);
                    n2 = String.valueOf((int) (10 + Math.random() * (90 - n0)));
                } else if (g == 1) {
    
    
                    guanxi1.setText("-");
                    n0 = (int) (10 + Math.random() * 90);
                    n1 = String.valueOf(n0);
                    n2 = String.valueOf((int) (10 + Math.random() * n0));
                } else if (g == 2) {
    
    
                    guanxi1.setText("*");
                } else if (g == 3) {
    
    
                    guanxi1.setText("/");
                }
                number1.setText(n1);
                number2.setText(n2);

                top.setText(t);
                an.setText("");


            } else {
    
    
//            ToastUtil.showMsg(getApplicationContext(),"已获取数据num1="+num1);
//            ToastUtil.showMsg(getApplicationContext(),"已获取数据num2="+num2);
//            ToastUtil.showMsg(getApplicationContext(),"已获取数据啊="+math);
                playVibrate(getApplicationContext(), false);
                ToastUtil.showMsg(getApplicationContext(), fail);
                an.setText("");


            }
        }

    }

    //结束按钮
    public void onClick2(View view) {
    
    

    }

    public int Math(int num1, String ch, int num2) {
    
    
        int num3 = 0;
        if (ch.equals("+")) num3 = num1 + num2;
        else if (ch.equals("-")) num3 = num1 - num2;
        else if (ch.equals("*")) num3 = num1 * num2;
        else if (ch.equals("/")) num3 = num1 / num2;
        return num3;
    }

    public static boolean isNumeric(String str) {
    
    
        for (int i = str.length(); --i >= 0; ) {
    
    
            if (!Character.isDigit(str.charAt(i))) {
    
    
                return false;
            }
        }
        return true;

    }

    /**
     * 手机震动
     *
     * @param context
     * @param isRepeat 是否重复震动
     */
    public static void playVibrate(Context context, boolean isRepeat) {
    
    

        /*
         * 设置震动,用一个long的数组来表示震动状态(以毫秒为单位)
         * 如果要设置先震动1秒,然后停止0.5秒,再震动2秒则可设置数组为:long[]{1000, 500, 2000}。
         * 别忘了在AndroidManifest配置文件中申请震动的权限
         */
        try {
    
    
            Vibrator mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            long[] patern = new long[]{
    
    2000, 500, 2000};
            AudioAttributes audioAttributes = null;
            /**
             * 适配android7.0以上版本的震动
             * 说明:如果发现5.0或6.0版本在app退到后台之后也无法震动,那么只需要改下方的Build.VERSION_CODES.N版本号即可
             */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    
    
                audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_ALARM) //key
                        .build();
                mVibrator.vibrate(patern, isRepeat ? 1 : -1, audioAttributes);
            } else {
    
    
                mVibrator.vibrate(patern, isRepeat ? 1 : -1);
            }
        } catch (Exception ex) {
    
    
            ex.printStackTrace();
        }
    }

I won’t post the layout, it’s a linear layout, just for fun, maybe… there’s a second one (if the kids aren’t so irritating…)

Guess you like

Origin blog.csdn.net/m0_54765221/article/details/128860578