我正在尝试构建一个 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;
}
}