按下按钮时清除edittext

时间:2012-08-12 02:27:32

标签: java android textview android-edittext textwatcher

我希望我的onTextChanged类在用户输入“空格”时读取,以便我可以清除我的EditText视图。问题是当按下空格键时没有任何反应。有谁知道我做错了什么?我的程序没有崩溃,它什么也没做。

 public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                SS.setText(s);
                if (s.equals("r")) {
                    SS.setText(s);
                    et.setText(" ");
                }
            }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    et.setTextColor(Color.parseColor("#000000"));

                }

                @Override
                public void afterTextChanged(final Editable s) {
                    // cancel the previous search if any

                    // toasted();
                    if (delayedAction != null) {
                        handler.removeCallbacks(delayedAction);
                    }
                    // toasted();

                    // define a new search
                    delayedAction = new Runnable() {

                        @Override
                        public void run() {
                            // start your search
                            // toasted();

                            // if (s.toString().equals(current)) {
                            // // toasted(); <== Here is where it needs to work
                            // // Toast.LENGTH_LONG).show();
                            // }

                            et.setTextColor(Color.parseColor("#C0C0C0"));
                            toasted();
                            tv.setText(et.getText().toString());
                            tv.setTextColor(Color.parseColor("#66CC66"));
                            et.setText("");
                        }

                    };

2 个答案:

答案 0 :(得分:1)

应该是s.equals(" ")而不是s.equals(' ')

答案 1 :(得分:0)

你可以尝试一下。

Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();