java - 如何使用数字键盘输入键执行方程?
问题描述
我正在尝试使用 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));
}
}
}
任何建议表示赞赏。
解决方案
你可以这样做
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;
}
});
推荐阅读
- docker - 在 golang 中导入存档/tar 时 docker run 失败
- applescript - Applescript选择滚动区域中的所有复选框
- java - 当一个项目依赖于其他项目时如何使用gradle构建jar
- android - 应用程序构造通过无法识别的参数在 toolchain.py 中生成错误
- java - unicode-16 中的 nextLine() 和 while 循环错误行为
- reactjs - 如何处理 redux saga 的错误
- javascript - 为什么我会收到“意外使用 'location' no-restricted-globals”?
- python - 在 Python 列表中使用一个字符串两次
- gcc - 为什么 `bool` 数据类型会使我的数据结构对 avr-gdb 不可读?
- python - 将 TriangulatePoints 与 SFM 与 Python 结合使用