我怎么知道EditText何时失去焦点? [英] How can I know when an EditText loses focus?

查看:143
本文介绍了我怎么知道EditText何时失去焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EditText失去焦点时,我需要抓住,我搜索了其他问题,但没有找到答案.

I need to catch when an EditText loses focus, I've searched other questions but I didn't find an answer.

我这样使用OnFocusChangeListener

OnFocusChangeListener foco = new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub

    }
};

但是,它对我不起作用.

But, it doesn't work for me.

推荐答案

setOnFocusChangeListener的实现onFocusChange,并且hasFocus有一个布尔参数.如果这是错误的,则您将注意力转移到另一个控件上.

Implement onFocusChange of setOnFocusChangeListener and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.

 EditText txtEdit = (EditText) findViewById(R.id.edittxt);

 txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {          
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
               // code to execute when EditText loses focus
            }
        }
    });

这篇关于我怎么知道EditText何时失去焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆