Android Edittext 显示光标 获取焦点 监听焦点
Edittext java 代码控制获取焦点
- EditText mEditText = (EditText) findViewById(R.id.et);
- mEditText.setFocusable(true);
- mEditText.setFocusableInTouchMode(true);
显示光标
- mEditText.requestFocus();
失去焦点
监听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 即可
希望对大家有用