How can I detect focused EditText in android?

前端 未结 8 1053
轮回少年
轮回少年 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.

提交回复
热议问题