首页 > 解决方案 > 如何使用数字键盘输入键执行方程?

问题描述

我正在尝试使用 java 和 android studio 制作我的第一个 apk,但我已经被困了几个小时。基本上它只是做一些数学运算。我开始的教程实现了一个计算按钮来根据用户输入执行方程,但我真的想使用键盘上的回车键并消除计算按钮,或者至少在按下回车时执行计算按钮。使用数字小键盘的回车键可让您进入下一行用户输入。添加所有输入后,它会变为复选标记,但按下它只会隐藏键盘。它不做数学运算并提供结果。我希望有人能指出我的仪式方向。

package com.sativa.beeralator;

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

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Objects;

import static android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {

    private EditText input5;
    private EditText input1;
    private EditText input2;
    private EditText input3;
    private EditText input4;
    private TextView tv_result;
    private TextView tv_result2;
    private String GetString;

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

        input5 = findViewById(R.id.et_input5);
        input1 = findViewById(R.id.et_input1);
        input2 = findViewById(R.id.et_input2);
        input3 = findViewById(R.id.et_input3);
        input4 = findViewById(R.id.et_input4);

        Button bt_calculate = findViewById(R.id.bt_calculate);

        tv_result = findViewById(R.id.tv_result);
        tv_result2 = findViewById(R.id.tv_result2);

        bt_calculate.setOnClickListener(new OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View view) {
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0);
                makeCalculations();


            }
        });
    }

    private void makeCalculations() {
            double doorfee = Double.parseDouble(input5.getText().toString());
            double people = Double.parseDouble(input1.getText().toString());
            double beersperhour = Double.parseDouble(input2.getText().toString());
            double hours = Double.parseDouble(input3.getText().toString());
            double contributions = doorfee * people;
            double cost = Double.parseDouble(input4.getText().toString());
            double result = Math.ceil(people * beersperhour * hours / 24);
            double result2 = (result * cost) - contributions;

            String sValue = String.format("%,.0f", result);
            String sValue2 = String.format("%,.2f", (Math.abs(result2)));

            tv_result.setText(String.format(" Required cases = %s", sValue));

            if (result2 >= 0) {
                tv_result2.setText(String.format(" Cost = $%s", sValue2));
            } else {
                tv_result2.setText(String.format(" Profit = $%s", sValue2));
            }

        }

}

任何建议表示赞赏。

标签: javaandroidinputkeypad

解决方案


你可以这样做

input5.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // do your stuff here
        }
        return false;
    }
});

推荐阅读