简体   繁体   English

我如何知道 EditText 何时失去焦点?

[英]How can I know when an EditText loses focus?

I need to catch when an EditText loses focus, I've searched other questions but I didn't find an answer.EditText失去焦点时,我需要抓住,我搜索了其他问题,但没有找到答案。

I used OnFocusChangeListener like this我像这样使用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.但是,它对我不起作用。

Implement onFocusChange of setOnFocusChangeListener and there's a boolean parameter for hasFocus.实现onFocusChangesetOnFocusChangeListener并且 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
            }
        }
    });

Have your Activity implement OnFocusChangeListener() if you want a factorized use of this interface, example:如果你想要这个接口的分解使用,让你的Activity实现OnFocusChangeListener() ,例如:

public class Shops extends AppCompatActivity implements View.OnFocusChangeListener{

In your OnCreate you can add a listener for example:在您的OnCreate您可以添加一个侦听器,例如:

editTextResearch.setOnFocusChangeListener(this);
editTextMyWords.setOnFocusChangeListener(this);
editTextPhone.setOnFocusChangeListener(this);

then android studio will prompt you to add the method from the interface, accept it... it will be like:然后android studio会提示你从界面中添加方法,接受它......它会像:

@Override
public void onFocusChange(View v, boolean hasFocus) {
// todo your code here...
}

and as you've got a factorized code, you'll just have to do that:并且当你有一个分解的代码时,你只需要这样做:

@Override
public void onFocusChange(View v, boolean hasFocus) {
   // todo: get your previous values to compare the new ones
   // They should exclude "" or get back to previous state...
   if (hasFocus) {
        editTextResearch.setText("");
        editTextMyWords.setText("");
        editTextPhone.setText("");
    }
    // in case it loses focus, do a better job getting back 
    // the previous value if no changes where made there:
    if (!hasFocus){
        editTextResearch.setText("BlaBlaBla");
        editTextMyWords.setText(" One Two Tree!");
        editTextPhone.setText("\"your phone here:\"");
    }
}

anything you code in the !hasFocus is for the behavior of the item that loses focus, that should do the trick!您在!hasFocus编写的任何代码都是针对失去焦点的项目的行为,这应该可以解决问题! But beware that in such state, the change of focus might overwrite the user's entries!但请注意,在这种状态下,焦点的变化可能会覆盖用户的条目!

Kotlin way科特林方式

editText.setOnFocusChangeListener { _, hasFocus ->
    if (!hasFocus) {  }
}

Its Working Properly其工作正常

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

et_mobile.setOnFocusChangeListener(new OnFocusChangeListener() {          
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            // code to execute when EditText loses focus
            if (et_mobile.getText().toString().trim().length() == 0) {
                CommonMethod.showAlert("Please enter name", FeedbackSubmtActivity.this);
            }
        }
    }
});



public static void showAlert(String message, Activity context) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(message).setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });
    try {
        builder.show();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Using Java 8 lambda expression:使用 Java 8 lambda 表达式:

editText.setOnFocusChangeListener((v, hasFocus) -> {
    if(!hasFocus) {
        String value = String.valueOf( editText.getText() );
    }        
});

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

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