How can I detect focused EditText in android?

前端 未结 8 1040
轮回少年
轮回少年 2020-12-05 06:24

I have a number of EditTexts in my page along with two buttons. I want the user to touch on any one EditText field and click any button to insert a

相关标签:
8条回答
  • 2020-12-05 07:18

    You can use View.OnFocusChangeListener to detect if any view (edittext) gained or lost focus.

    for (EditText view : editList){
       view.setOnFocusChangeListener(focusListener);
    }
    ....
    private OnFocusChangeListener focusListener = new OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus){
                 focusedView = v;
            } else {
                 focusedView  = null;
            }
        }
    }
    

    This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class. And obviously you would need to create an array with them in it, thus the editList.

    0 讨论(0)
  • 2020-12-05 07:20

    Simple Coding:

    if(EditText1.isFocused()){
      //EditText1 is focused
    }else if(EditText2.isFocused()){
      //EditText2 is focused
    }
    
    0 讨论(0)
提交回复
热议问题