我有一个EditText,并且有一个TextWatcher

我在其中输入数字,如果删除了最后一个文本,我想用0填充该字段。

如何使用TextWatcher的三种方法?

    input.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            double value=Double.parseDouble(input.getText().toString());//here it will throw error if no text there.
            //I do not only want to catch this exception and do something with it, but I want to detect this event and if it happens I want to try some solution to stop it.
        }
    });

最佳答案

在afterTextChanged方法中...如果要查看的块是否为空。另外,也可以使用方法参数中包含的Editable代替输入editbox。

if (s.toString()!=null && s.toString().trim().equals("")==false){
   double value=Double.parseDouble(s.getText().toString());
}else{
   double value = 0;
}

10-07 19:36
查看更多