Disable EditText blinking cursor

前端 未结 10 2009
耶瑟儿~
耶瑟儿~ 2020-11-28 02:27

Does anyone know how to disable the blinking cursor in an EditText view?

10条回答
  •  感动是毒
    2020-11-28 02:56

    Perfect Solution that goes further to the goal

    Goal: Disable the blinking curser when EditText is not in focus, and enable the blinking curser when EditText is in focus. Below also opens keyboard when EditText is clicked, and hides it when you press done in the keyboard.

    1) Set in your xml under your EditText:

    android:cursorVisible="false"
    

    2) Set onClickListener:

    iEditText.setOnClickListener(editTextClickListener);
    
    OnClickListener editTextClickListener = new OnClickListener() 
    
    {
    
        public void onClick(View v) 
        {
             if (v.getId() == iEditText.getId()) 
            {
                iEditText.setCursorVisible(true);
            }
    
        }
    };
    

    3) then onCreate, capture the event when done is pressed using OnEditorActionListener to your EditText, and then setCursorVisible(false).

    //onCreate...
    
    iEditText.setOnEditorActionListener(new OnEditorActionListener() {
    
                @Override
                public boolean onEditorAction(TextView v, int actionId,
                        KeyEvent event) {
                        iEditText.setCursorVisible(false);
                    if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                        InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        in.hideSoftInputFromWindow(iEditText.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                    return false;
                }
            });
    

提交回复
热议问题