java - 检查特定字符串的 EditText 内容
问题描述
大家,
我在网上找了很久,但我找不到解决办法-所以我直接问专业人士,哈哈。我的问题是我想检查 EditText 的字符。我正在编写一个计算器,这里不应该在 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);
}
推荐阅读
- python-3.x - Vimeo API - 我如何从评论 URL 中获取注释
- android - Flutter google_sign_in 登录失败,没有代码错误
- python - Conda env 与 venv / pyenv / virtualenv / 等
- nsis - 在 NSIS 中使用引号中的 binpath 创建服务
- c# - Aspose.svg 的内存泄漏
- css - 你可以在 React js 文件中导入外部 CSS 文件吗?
- python - AttributeError:“QCoreApplication”对象没有属性“setQuitOnLastWindowClosed”
- python - 如何在 conda env 中安装需要共享库的 python 包?
- google-apps-script - Can a script be added to a Google Slide allowing it to advance to the next slide only with a specific alphanumeric input in a text box?
- javascript - Why on earth is not this return statement executed here in my codesandbox UseEffect