Event for Handling the Focus of the EditText

后端 未结 4 1090
时光取名叫无心
时光取名叫无心 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: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.

提交回复
热议问题