简体   繁体   English

如何检测Edittext是否失去焦点?

[英]How can I detect if an Edittext loses focus?

I want to be able to detect when an edittext isn't clicked, so I can replace it with a textview. 我希望能够检测何时未单击edittext,因此可以将其替换为textview。 I tried 我试过了

et.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!v.hasFocus()){
                    Toast.makeText(getApplicationContext(), "LOST FOCUS", Toast.LENGTH_SHORT).show();
                }
            }
        });

but the Toast message wouldn't get called. 但是Toast消息不会被调用。

You should use the boolean var that is supplied: 您应该使用提供的布尔变量:

if(!hasFocus){

When you get the callback, it could be that the view still has focus, so v.hasFocus() might return true. 当您获得回调时,可能是视图仍然具有焦点,因此v.hasFocus()可能返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM