检查特定字符串的编辑文本内容



everyone,

我在网上找了很久,但找不到解决方案——所以我直接问专业人士,哈哈。 我的问题是我想检查字符的编辑文本。我正在编写一个计算器,在这里不应该发生在 EditText 中显示多个计算运算符的情况。我当然可以以不同的方式解决此问题,但是是否有一个函数可以检查 EditText 中的字符串?

在这里,整个计算被写入EditText。

谢谢!

试试这个函数..把它放在运算符的每个侦听器中


private void checkOperators(){

//change the id with the id of your EditText
EditText myEditText = findViewById(R.id.idEditText);
//Get the last char
String lastChar = myEditText.getText().toString().substring(myEditText.length());
if (lastChar.equals("+") || lastChar.equals("*") || lastChar.equals("-") || lastChar.equals("/")){
//Delete the first operator if they exist
myEditText.setText(myEditText.getText().toString().substring(0,myEditText.length()-1);
}

// replace "newOperator" with your operator
myEditText.setText(myEditText.getText().append(newOperator);
}

最新更新