首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓EditText -完成打字事件

安卓EditText -完成打字事件
EN

Stack Overflow用户
提问于 2011-11-09 18:13:57
回答 14查看 162.4K关注 0票数 132

我想在用户编辑完EditText时捕获一个事件。

怎么做呢?

EN

回答 14

Stack Overflow用户

回答已采纳

发布于 2011-11-09 18:19:52

用户完成编辑后,将按DoneEnter

((EditText)findViewById(R.id.youredittext)).setOnEditorActionListener(
    new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                    actionId == EditorInfo.IME_ACTION_DONE ||
                    event != null &&
                    event.getAction() == KeyEvent.ACTION_DOWN &&
                    event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                if (event == null || !event.isShiftPressed()) {
                   // the user is done typing. 

                   return true; // consume.
                }                
            }
            return false; // pass on to other listeners. 
        }
    }
);
票数 125
EN

Stack Overflow用户

发布于 2015-02-20 17:36:56

我个人更喜欢在打字结束后自动提交。下面是如何检测此事件的方法。

声明和初始化:

private Timer timer = new Timer();
private final long DELAY = 1000; // in ms

监听程序,例如onCreate()

EditText editTextStop = (EditText) findViewById(R.id.editTextStopId);
    editTextStop.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }
        @Override
        public void onTextChanged(final CharSequence s, int start, int before,
                int count) {
            if(timer != null)
                timer.cancel();
        }
        @Override
        public void afterTextChanged(final Editable s) {
            //avoid triggering event when text is too short
            if (s.length() >= 3) {              

                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        // TODO: do what you need here (refresh list)
                        // you will probably need to use
                        // runOnUiThread(Runnable action) for some specific
                        // actions
                        serviceConnector.getStopPoints(s.toString());
                    }

                }, DELAY);
            }
        }
    });

因此,当文本被更改时,计时器将开始等待下一次更改的发生。当它们发生时,计时器被取消,然后再次启动。

票数 68
EN

Stack Overflow用户

发布于 2011-11-09 18:25:40

您可以使用setOnKeyListener或如下所示的textWatcher完成此操作:

设置文本观察器editText.addTextChangedListener(textWatcher);

然后调用

private TextWatcher textWatcher = new TextWatcher() {

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

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

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8063439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档