【发布时间】:2019-12-30 17:53:39
【问题描述】:
我正在一个 Android XML 应用程序中创建一个 Java 计算器(仅按钮 - 像 T9 键),我想修复“取消”按钮。
当我单击它时,我想取消仅包含数字的字符串中的最后一个数字(或“.”,因为您也可以添加“.”来写入十进制数字)。 有一个问题:如果用户写了一个 int 数(34),我把它转换成 double,它就变成了 34.0,如果我取消最后一位,它就取消了 0。
我也试过String.substring(start,end),但它不起作用......
您对取消句柄有什么建议吗?谢谢!
这是取消最后一位数字的函数。
/* tv -> TextView
- tvResult contains what the user writes and the result when he clicks "=", - tvCalc contains the calc that the user is entering.
For example, if the user writes with the 0-9 buttons and the operator of the operations, in tvCalc there will be "34+50=", and in tvResult "84" */
public void handleCancel(View v) {
//tv -> TextView, tvResult contains what the user writes and the result when he clicks "=", tvCalc contains the calc that the user is entering: for example if user writes with the 0-9 buttons and the operator of the operations, in tvCalc there will be "34+50=", and in tvResult "84"
if (tvResult.length() == 0) {
errorToast = Toast.makeText(MainActivity.this, "Error! There's nothing to cancel!", Toast.LENGTH_LONG);
errorToast.show();
} else {
tvResult.setText(tvResult.toString().substring(0, tvResult.toString().length() - 1))
if (tvCalc.toString().contains("=")) {
tvCalc.setText(tvResult.toString());
operand1 = tvResult.toString();
} else {
tvCalc.setText(tvCalc.toString().substring(0, tvCalc.toString().length() - 1));
if (operator == "") operand1 = tvResult.toString();
else {
operand2 = tvResult.toString();
try {
int conv = Integer.parseInt(tvCalcolo.toString());
operazione = "";
} catch(Exception e) {}
}
}
}
}
添加用户选择的功能:
public void userChoice(View v)
{
Button clicked=(Button)findViewById(v.getId());
String choice=clicked.getText().toString();
try
{
int number=Integer.parseInt(choice);
if (operator=="")
{
operand1+=choice;
if (tvCalc.toString().contains("=")) tvCalc.setText(operand1);
} else operand2+=choice;
} catch (Exception e)
{
if (choice.equals('.'))
{
if (operator=="") operand1+=choice;
else operand2+=choice;
} else
{
if (operand2!="")
{
handleEqual(v);
operand1=tvResult.toString();
}
operator=choice;
tvCalc.append(operator);
tvResult.setText("");
return;
}
}
tvCalc.append(choice);
tvResult.append(choice);
}
感谢 Cardinal System,我编辑了函数“handleCancel”:
public void handleCancel (View v)
{
double d;
try
{
d=Double.parseDouble(tvRisultato.toString());
} catch (Exception e)
{
bCanc.setText("Error"); //It's the button 'Cancel'
}
String newVal;
if (d%1==0)
{
int val=(int)d;
newVal=String.valueOf(val);
newVal=newVal.substring(0,newVal.length()-1);
} else
{
newVal=String.valueOf(d);
newVal=newVal.substring(0,newVal.length()-1);
if (newVal.endsWith("."))
{
newVal=newVal.substring(0,newVal.length()-1);
}
}
tvResult.setText(newVal);
if ((tvCalc.toString().contains("+")||tvCalc.toString().contains("-")||tvCalc.toString().contains("*")||tvCalc.toString().contains("/")||tvCalc.toString().contains("^"))&&tvCalc.toString().contains("="))
{
tvCalc.setText(tvResult.toString());
operand1=tvResult.toString();
} else if ((tvCalc.toString().contains("+")||tvCalc.toString().contains("-")||tvCalc.toString().contains("*")||tvCalc.toString().contains("/")||tvCalc.toString().contains("^"))&&!tvCalc.toString().contains("="))
{
if (tvCalc.toString().endsWith("+")||tvCalc.toString().endsWith("-")||tvCalc.toString().endsWith("*")||tvCalc.toString().endsWith("/")||tvCalc.toString().endsWith("^")) operator="";
tvCalc.setText(tvCalc.toString().substring(0,tvCalc.toString().length()-1));
}
}
【问题讨论】:
-
请发布您的代码。
-
如果用户自己没有添加小数点,那么在选择 = 按钮之前也不要以编程方式添加它。 取消应始终与用户实际输入的内容相反,否则需要更多代码。
-
tvResult是 TextView 吗? -
是的,
tvResult是一个 TextView
标签: java string substring calculator