getCompoundDrawables

前端之家收集整理的这篇文章主要介绍了getCompoundDrawables前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

getCompoundDrawables

方法简介:

用于获取当下该控件的上下左右位置的Drawable引用,该方法返回的是Drawable[]数组,length为4,分别对应Left,Top,Right和Bottom的Drawable引用。比如:以下代码的Edittext,drawable[2]就对应于EditText控件的右端。在代码中动态添加图标就用到这个方法,相当于XML文件中的drawableRight。



代码


  1. public class ClearableEditText extends EditText implements View.OnTouchListener,View.OnFocusChangeListener,TextWatcher {
  2.  
  3. public interface Listener {
  4. void didClearText();
  5. }
  6.  
  7. public void setListener(Listener listener) {
  8. this.listener = listener;
  9. }
  10.  
  11. private Drawable xD;
  12. private Listener listener;
  13.  
  14. public ClearableEditText(Context context) {
  15. super(context);
  16. init();
  17. }
  18.  
  19. public ClearableEditText(Context context,AttributeSet attrs) {
  20. super(context,attrs);
  21. init();
  22. }
  23.  
  24. public ClearableEditText(Context context,AttributeSet attrs,int defStyle) {
  25. super(context,attrs,defStyle);
  26. init();
  27. }
  28.  
  29. @Override
  30. public void setOnTouchListener(OnTouchListener l) {
  31. this.l = l;
  32. }
  33.  
  34. @Override
  35. public void setOnFocusChangeListener(OnFocusChangeListener f) {
  36. this.f = f;
  37. }
  38.  
  39. private OnTouchListener l;
  40. private OnFocusChangeListener f;
  41.  
  42. @Override
  43. public boolean onTouch(View v,MotionEvent event) {
  44. if (getCompoundDrawables()[2] != null) {
  45. boolean tappedX = event.getX() > (getWidth() - getPaddingRight() - xD
  46. .getIntrinsicWidth());
  47. if (tappedX) {
  48. if (event.getAction() == MotionEvent.ACTION_UP) {
  49. setText("");
  50. if (listener != null) {
  51. listener.didClearText();
  52. }
  53. }
  54. return true;
  55. }
  56. }
  57. if (l != null) {
  58. return l.onTouch(v,event);
  59. }
  60. return false;
  61. }
  62.  
  63. @Override
  64. public void onFocusChange(View v,boolean hasFocus) {
  65. if (hasFocus) {
  66. setClearIconVisible(getText().length() > 0);
  67. } else {
  68. setClearIconVisible(false);
  69. }
  70. if (f != null) {
  71. f.onFocusChange(v,hasFocus);
  72. }
  73. }
  74.  
  75. @Override
  76. public void onTextChanged(CharSequence s,int start,int before,int count) {
  77. if (isFocused()) {
  78. setClearIconVisible(getText().length() > 0);
  79. }
  80. }
  81.  
  82. @Override
  83. public void beforeTextChanged(CharSequence s,int count,int after) {
  84.  
  85. }
  86.  
  87. @Override
  88. public void afterTextChanged(Editable s) {
  89.  
  90. }
  91.  
  92. private void init() {
  93. xD = getCompoundDrawables()[2];
  94. if (xD == null) {
  95. xD = getResources()
  96. .getDrawable(android.R.drawable.presence_offline);
  97. }
  98. xD.setBounds(0,0,xD.getIntrinsicWidth(),xD.getIntrinsicHeight());
  99. setClearIconVisible(false);
  100. super.setOnTouchListener(this);
  101. super.setOnFocusChangeListener(this);
  102. addTextChangedListener(this);
  103. }
  104.  
  105. protected void setClearIconVisible(boolean visible) {
  106. boolean wasVisible = (getCompoundDrawables()[2] != null);
  107. if (visible != wasVisible) {
  108. Drawable x = visible ? xD : null;
  109. setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],x,getCompoundDrawables()[3]);
  110. }
  111. }
  112.  
  113. }

拓展:

登录界面经常用到密码的显示和隐藏按钮,实现如下代码


  1. loginPswEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码
  2. loginPswEt.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码

此至!

猜你在找的XML相关文章