【问题标题】:Why my app stops when I delete all from EditText?当我从 EditText 中删除所有内容时,为什么我的应用程序停止了?
【发布时间】:2022-01-24 08:50:43
【问题描述】:

我想检查 EditText 的值是否大于 5,我希望 edittext 的背景变为红色,但它会在我删除文本时停止。请帮忙。

  vibration.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            vibration.setBackgroundColor(Color.YELLOW);
        }

        @Override
        public void afterTextChanged(Editable s) {
            float numV = Float.parseFloat(vibration.getText().toString());
            if(numV > 5){
                vibration.setBackgroundColor(Color.RED);
            }else{
                return;
            }
        }
    });

【问题讨论】:

  • 空文本不是数字给出空引号“”,启用 parseFloat 异常即将到来。
  • 所以我应该处理这个异常?
  • 可以检查字符串是否为空然后解析成float,否则返回

标签: java android-studio


【解决方案1】:

首先检查您从 EditText 获得的值是否不为空。

if(!yourEditText.isempty)
{
//Do your work here
}
else{
//EditText is null}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多