我怎么知道 EditText 何时失去焦点?

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

本文介绍了我怎么知道 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.

推荐答案

实现setOnFocusChangeListeneronFocusChange,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 何时失去焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我怎么知道 EditText 何时失去焦点?

基础教程推荐