android 100以内加减法app(一)

在这里插入图片描述
t1t1t1t1

写在前面

只前没自己独立写过app,这个也没用什么框架,纯属于给家里小孩做着玩的,

功能

进入app后,直接就是两个问号的界面
点击开始
出现题,后台其实写了有乘法除法,但一方面小孩还没学到乘法除法,另一方面……超100了
写对了会有提示,
写错了会有提示并且震动

部分要修正的功能或者bug

1.连续点击按钮会闪退
2.结束按钮还没想好做啥
3.数据没有持久化
4.界面太丑了……

代码

  //声明控件
    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();
        }
    }

布局就不贴了,线性布局,纯自己写着玩,可能……有第二篇吧(如果小孩不那么气人的话……)

猜你喜欢

转载自blog.csdn.net/m0_54765221/article/details/128860578