gpt4 book ai didi

java - 检查 EditText 内容中的特定字符串

转载 作者:行者123 更新时间:2023-12-02 00:49:45 24 4
gpt4 key购买 nike

大家

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

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

谢谢!

最佳答案

尝试这个函数..将其放入运算符(operator)的每个监听器中


private void checkOperators(){<p></p>

<pre><code> //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);

}
</code></pre>

<p></p>

关于java - 检查 EditText 内容中的特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57869730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com