我正在尝试构建一个Android计算器应用程序。我正在尝试使用按钮创建类似“351 + 193 * 123”的字符串。为了防止语法错误,我必须排除相邻的运算符和十进制(+, - ,*,/ ,.)。但是当我测试应用程序时,仍然可以输入双操作符。请查看私人Button.onClicklistener - > switch(v.getId()) - > case btnPlus,btnMinus,btnTimes,btnDivide,btnDot
package com.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CalculatorActivity extends Activity {
private EditText edtShow;
private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9,
btnPlus, btnMinus, btnTimes, btnDivide, btnEquals, btnDot, btnClear, btnBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
edtShow = (EditText)findViewById(R.id.edtShow);
btn0 = (Button)findViewById(R.id.btn0);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
btn3 = (Button)findViewById(R.id.btn3);
btn4 = (Button)findViewById(R.id.btn4);
btn5 = (Button)findViewById(R.id.btn5);
btn6 = (Button)findViewById(R.id.btn6);
btn7 = (Button)findViewById(R.id.btn7);
btn8 = (Button)findViewById(R.id.btn8);
btn9 = (Button)findViewById(R.id.btn9);
btnPlus = (Button)findViewById(R.id.btnPlus);
btnMinus = (Button)findViewById(R.id.btnMinus);
btnTimes = (Button)findViewById(R.id.btnTimes);
btnDivide = (Button)findViewById(R.id.btnDivide);
btnEquals = (Button)findViewById(R.id.btnEquals);
btnDot = (Button)findViewById(R.id.btnDot);
btnClear = (Button)findViewById(R.id.btnClear);
btnBack = (Button)findViewById(R.id.btnBack);
btn0.setOnClickListener(myListner);
btn1.setOnClickListener(myListner);
btn2.setOnClickListener(myListner);
btn3.setOnClickListener(myListner);
btn4.setOnClickListener(myListner);
btn5.setOnClickListener(myListner);
btn6.setOnClickListener(myListner);
btn7.setOnClickListener(myListner);
btn8.setOnClickListener(myListner);
btn9.setOnClickListener(myListner);
btnPlus.setOnClickListener(myListner);
btnMinus.setOnClickListener(myListner);
btnTimes.setOnClickListener(myListner);
btnDivide.setOnClickListener(myListner);
btnEquals.setOnClickListener(myListner);
btnDot.setOnClickListener(myListner);
btnClear.setOnClickListener(myListner);
btnBack.setOnClickListener(myListner);
}
private Button.OnClickListener myListner = new Button.OnClickListener()
{
public void onClick (View v)
{
String s = edtShow.getText().toString();
switch (v.getId())
{
case R.id.btn0:
{
if(s.length() != 0)
{
edtShow.setText(s + 0);
}
break;
}
case R.id.btn1:
{
edtShow.setText(s + 1);
break;
}
case R.id.btn2:
{
edtShow.setText(s + 2);
break;
}
case R.id.btn3:
{
edtShow.setText(s + 3);
break;
}
case R.id.btn4:
{
edtShow.setText(s + 4);
break;
}
case R.id.btn5:
{
edtShow.setText(s + 5);
break;
}
case R.id.btn6:
{
edtShow.setText(s + 6);
break;
}
case R.id.btn7:
{
edtShow.setText(s + 7);
break;
}
case R.id.btn8:
{
edtShow.setText(s + 8);
break;
}
case R.id.btn9:
{
edtShow.setText(s + 9);
break;
}
case R.id.btnPlus:
{
if(s.substring(s.length() - 1) != "+"
&& s.substring(s.length() - 1) != "-"
&& s.substring(s.length() - 1) != "*"
&& s.substring(s.length() - 1) != "/"
&& s.substring(s.length() - 1) != ".")
{
edtShow.setText(s + s.substring(s.length() - 1));
}
break;
}
case R.id.btnMinus:
{
if(s.substring(s.length() - 1) != "+"
&& s.substring(s.length() - 1) != "-"
&& s.substring(s.length() - 1) != "*"
&& s.substring(s.length() - 1) != "/"
&& s.substring(s.length() - 1) != ".")
{
edtShow.setText(s + "-");
}
break;
}
case R.id.btnTimes:
{
if(s.substring(s.length() - 1) != "+"
&& s.substring(s.length() - 1) != "-"
&& s.substring(s.length() - 1) != "*"
&& s.substring(s.length() - 1) != "/"
&& s.substring(s.length() - 1) != ".")
{
edtShow.setText(s + "*");
}
break;
}
case R.id.btnDivide:
{
if(s.substring(s.length() - 1) != "+"
&& s.substring(s.length() - 1) != "-"
&& s.substring(s.length() - 1) != "*"
&& s.substring(s.length() - 1) != "/"
&& s.substring(s.length() - 1) != ".")
{
edtShow.setText(s + "/");
}
break;
}
case R.id.btnDot:
{
if(s.substring(s.length() - 1) != "+"
&& s.substring(s.length() - 1) != "-"
&& s.substring(s.length() - 1) != "*"
&& s.substring(s.length() - 1) != "/"
&& s.substring(s.length() - 1) != ".")
{
edtShow.setText(s + ".");
}
break;
}
case R.id.btnClear:
{
edtShow.setText("");
break;
}
case R.id.btnBack:
{
edtShow.setText(s.substring(0, s.length() - 1));
break;
}
case R.id.btnEquals:
{
break;
}
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculator, menu);
return true;
}
}
答案 0 :(得分:3)
您正在使用!=比较字符串。这不是Java方式 - Java没有运算符重载。您应该使用string.equals
,如下所示:
if(!s.substring(0, s.length()-1).equals("+")) ...
那就是说,你的代码可以使用一些清理工具。所有数字按钮都相同,所有操作员按钮都相同 - 将它们分组并有两个事件处理程序 - 每种类型一个。您不需要为每个语句设置一个大的switch语句,只需使用视图标记来保存数字或运算符。
答案 1 :(得分:2)
您不必使用substring()方法,您可以使用charAt()方法,并且可以与“==”运算符进行比较。例如; -
char last=s.charAt(s.length()-1);
if(last == '+'){
// its plus
}else{
//it's not plus
}
答案 2 :(得分:0)
你可以使用
像这样.endsWith
if(!s.endsWith("+")){
// input "+"
}