Event for Handling the Focus of the EditText

后端 未结 4 1078
时光取名叫无心
时光取名叫无心 2020-12-02 05:53

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 m

相关标签:
4条回答
  • 2020-12-02 06:12

    when in kotlin it will look like this :

    editText.setOnFocusChangeListener { view, hasFocus ->
            if (hasFocus) toast("focused") else toast("focuse lose")
        }
    
    0 讨论(0)
  • 2020-12-02 06:12

    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();
    
                }
            }
        });
    
    0 讨论(0)
  • 2020-12-02 06:13

    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();
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-02 06:15
    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.

    0 讨论(0)
提交回复
热议问题