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

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. //      ┗┻┛ ┗┻┛  
希望对大家有用
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值