100안드로이드 덧셈과 뺄셈 앱 (1)

여기에 이미지 설명 삽입
t1t1t1t1

앞에 쓰여진

이전에 독립적으로 앱을 작성한 적이 없고, 이 앱은 프레임워크를 사용하지 않으며, 집에서 아이들이 가지고 놀기 위한 것입니다.

기능

앱에 들어가시면 물음표가 2개 있는 인터페이스가 나옵니다
클릭해서 시작하세요 문제가 있습니다 배경에는 곱셈과 나눗셈
있는데 한편으로는 아이가 곱셈과 나눗셈을 배우지 않았습니다
.
실수를 하면 프롬프트와 진동이 있을 것입니다.

일부 기능 또는 버그 수정

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();
        }
    }

레이아웃은 게시하지 않겠습니다. 선형 레이아웃입니다. 그냥 재미로 작성합니다. 어쩌면…

Supongo que te gusta

Origin blog.csdn.net/m0_54765221/article/details/128860578
Recomendado
Clasificación