微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用数字键盘的回车键执行方程式?

如何解决如何使用数字键盘的回车键执行方程式?

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

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

        }

}

任何建议表示赞赏。

解决方法

你可以这样做

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?