Android Edittext 显示光标 获取焦点 监听焦点

来源:互联网 发布:非衍生金融资产 知乎 编辑:程序博客网 时间:2024/06/12 20:41

Android Edittext 显示光标 获取焦点 监听焦点   



Edittext java 代码控制获取焦点  
[java] view plain copy
  1. EditText mEditText = (EditText) findViewById(R.id.et);  
  2. mEditText.setFocusable(true);  
  3. mEditText.setFocusableInTouchMode(true);  
显示光标
[java] view plain copy
  1. mEditText.requestFocus();//获取焦点 光标出现  

失去焦点
[java] view plain copy
  1. mEditText.clearFocus();  
监听EditText焦点变化   当获取焦点后 hasFocus 为true
[java] view plain copy
  1. mEditText.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {  
  2.   
  3.                 @Override  
  4.                 public void onFocusChange(View v, boolean hasFocus) {  
  5.   
  6.                     if (hasFocus) {  
  7.   
  8.                         // 获得焦点  
  9.   
  10.                     } else {  
  11.   
  12.                         // 失去焦点  
  13.   
  14.                     }  
  15.   
  16.                 }  
  17.   
  18.   
  19.             });  
使用XML配置文件控制光标的代码
cursorVisible 中
true为显示  
false为隐藏光标
[html] view plain copy
  1. android:cursorVisible="true"  
  2. android:cursorVisible="false"  
EditText不自动获取焦点 在EditText的父级控件上设置
[html] view plain copy
  1. android:focusable="true"  
  2. android:focusableInTouchMode="true"  
EditText 设置光标颜色
[html] view plain copy
  1. android:textCursorDrawable="#ff2244"  
如果想设置光标颜色和字体一样 设置@null 即可


[java] view plain copy
  1. //  ┏┓   ┏┓  
  2. //┏┛┻━━━┛┻┓  
  3. //┃       ┃  
  4. //┃   ━   ┃  
  5. //┃ ┳┛ ┗┳ ┃  
  6. //┃       ┃  
  7. //┃   ┻   ┃  
  8. //┃       ┃  
  9. //┗━┓   ┏━┛  
  10. //    ┃   ┃   神兽保佑  
  11. //    ┃   ┃   代码无BUG!  
  12. //    ┃   ┗━━━┓  
  13. //    ┃       ┣┓  
  14. //    ┃       ┏┛  
  15. //    ┗┓┓┏━┳┓┏┛  
  16. //      ┃┫┫ ┃┫┫  
  17. //      ┗┻┛ ┗┻┛  
希望对大家有用
原创粉丝点击