Event for Handling the Focus of the EditText

2019-01-16 02:24发布

Can anyone suggest me any event related to the focus of the EditText? My application contains a EditText, which accepts a URL in it.

Now my problem is that, that after the user will enter the URL in the field and Move further, without any of the click event, i.e when the focus will move from the EditText, it should detect the entered Url and goes to the server.

If I get the reply using Json Parsing, then it'll be more convenient.

3条回答
We Are One
2楼-- · 2019-01-16 02:57
  1. Declare object of EditText on top of class:

     EditText myEditText;
    
  2. Find EditText in onCreate Function and setOnFocusChangeListener of EditText:

    myEditText = findViewById(R.id.yourEditTextNameInxml); 
    
    myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    if (!hasFocus) {
                         Toast.makeText(this, "Focus Lose", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(this, "Get Focus", Toast.LENGTH_SHORT).show();
                    }
    
                }
            });
    

It works fine.

查看更多
贼婆χ
3楼-- · 2019-01-16 03:02

Here is the focus listener example.

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if (hasFocus) {
            Toast.makeText(getApplicationContext(), "Got the focus", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show();
        }
    }
});
查看更多
孤傲高冷的网名
4楼-- · 2019-01-16 03:18

For those of us who this above valid solution didnt work, there's another workaround here

 searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean isFocused) {
            if(!isFocused)
            {
                Toast.makeText(MainActivity.this,"not focused",Toast.LENGTH_SHORT).show();

            }
        }
    });
查看更多
登录 后发表回答