Java 需要一个android按钮来执行一个故意空白的编辑文本框
我有一个简单的屏幕,有两个编辑文本框和一个按钮。我希望用户能够在框中输入各种整数,程序将根据输入框中的特定数字执行不同的操作。我试图让他们一次只输入一个数字,但除非两个框中都有内容,否则代码似乎不会执行。我让if语句在执行之前检查各个框中的null,以确定要执行哪段代码Java 需要一个android按钮来执行一个故意空白的编辑文本框,java,android,android-button,Java,Android,Android Button,我有一个简单的屏幕,有两个编辑文本框和一个按钮。我希望用户能够在框中输入各种整数,程序将根据输入框中的特定数字执行不同的操作。我试图让他们一次只输入一个数字,但除非两个框中都有内容,否则代码似乎不会执行。我让if语句在执行之前检查各个框中的null,以确定要执行哪段代码 public void button(View view) { double x, y; EditText freq1 = findViewById(R.id.freq1); EditText fre
public void button(View view) {
double x, y;
EditText freq1 = findViewById(R.id.freq1);
EditText freq2 = findViewById(R.id.freq2);
TextView str1 = findViewById(R.id.freqanswer);
TextView str2 = findViewById(R.id.injVolt);
TextView error1 = findViewById(R.id.error1);
String strf1, strf2;
strf1 = freq1.getText().toString();
strf2 = freq2.getText().toString();
try {
f1 = Double.parseDouble(strf1);
f2 = Double.parseDouble(strf2);
if ((f1 >= 225) & (f1 <= 312) & (strf1.isEmpty())) {
x = f1 + 20.6;
y = x / 4;
str1.setText(String.format("%.3f", y));
}
}
catch (Exception e){
error1.setText("splat");
}
finally {
InputMethodManager input = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
公共作废按钮(视图){
双x,y;
EditText freq1=findViewById(R.id.freq1);
EditText freq2=findViewById(R.id.freq2);
TextView str1=findviewbyd(R.id.freqanswer);
TextView str2=findviewbyd(R.id.injVolt);
TextView error1=findViewById(R.id.error1);
字符串strf1,strf2;
strf1=freq1.getText().toString();
strf2=freq2.getText().toString();
试一试{
f1=Double.parseDouble(strf1);
f2=Double.parseDouble(strf2);
如果((f1>=225)和(f1我认为您应该在分配之前检查:
if(strf1.isEmpty()){strf1="0";} //if assuming zero does not change the formula's output
if(strf2.isEmpty()){strf2="0";}
f1 = Double.parseDouble(strf1);
f2 = Double.parseDouble(strf2);
通过这种方式,您可以确定默认值。空的edittext将返回空字符串”
而不是null
,您的问题是Double.parseDouble(“”)如果我要立即测试一个异常,您必须在分析之前进行检查您所说的是什么意思我试图让他们一次只输入一个数字
通过只取一个值来执行什么..请清楚地说明这一点,因为根据您的操作,错误检查代码可能会更改