Edittext java 代码控制获取焦点  
            EditText mEditText = (EditText) findViewById(R.id.et);
            mEditText.setFocusable(true);
            mEditText.setFocusableInTouchMode(true);

显示光标
mEditText.requestFocus();//获取焦点 光标出现

失去焦点
mEditText.clearFocus();
监听EditText焦点变化   当获取焦点后 hasFocus 为true
mEditText.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {

                    if (hasFocus) {

                        // 获得焦点

                    } else {

                        // 失去焦点

                    }

                }


            });


使用XML配置文件控制光标的代码
cursorVisible 中
true为显示  
false为隐藏光标
android:cursorVisible="true"
android:cursorVisible="false"

EditText不自己主动获取焦点 在EditText的父级控件上设置
                    android:focusable="true"
                    android:focusableInTouchMode="true"

EditText 设置光标颜色

                android:textCursorDrawable="#ff2244"
假设想设置光标颜色和字体一样 设置@null 就可以


//  ┏┓   ┏┓
//┏┛┻━━━┛┻┓
//┃       ┃
//┃   ━   ┃
//┃ ┳┛ ┗┳ ┃
//┃       ┃
//┃   ┻   ┃
//┃       ┃
//┗━┓   ┏━┛
//    ┃   ┃   神兽保佑
//    ┃   ┃   代码无BUG!
//    ┃   ┗━━━┓
//    ┃       ┣┓
//    ┃       ┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫ ┃┫┫
//      ┗┻┛ ┗┻┛

希望对大家实用

相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2022-02-08
  • 2022-01-01
  • 2021-08-27
猜你喜欢
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2021-12-08
  • 2021-06-14
相关资源
相似解决方案