在我在Activity中的应用程序中,我想设置EditText,因为我单击里面(焦点)EditText并键入一个键清除按钮应该出现在EditText的右侧,当EditText为空时,清除按钮必须删除.但它没有显示给我..我应该在这里实现哪些事件..?onTouch或onFocusChange或addTextChangedListener以及那里的代码..?以下代码我在活动中完成了……
在活动中:
clear = getResources().getDrawable(R.drawable.round_clear); clear.setBounds(0,clear.getIntrinsicWidth(),clear.getIntrinsicHeight());
和事件
@Override public void onFocusChange(View v,boolean hasFocus) { switch (v.getId()) { case R.id.uIDEditText: if(hasFocus && !uIDEditText.getText().toString().isEmpty()) uIDEditText.setCompoundDrawables(null,null,clear,null); else uIDEditText.setCompoundDrawables(null,null); break; case R.id.pwdEditText: if(hasFocus && !pwdEditText.getText().toString().isEmpty()) pwdEditText.setCompoundDrawables(null,null); else pwdEditText.setCompoundDrawables(null,null); break; } }
另一个事件是:
@Override public boolean onTouch(View v,MotionEvent event) { switch (v.getId()) { case R.id.uIDEditText: final int x = (int)event.getX(); final int y = (int)event.getY(); if(event.getAction() == MotionEvent.ACTION_UP && clear!=null) { Rect rBounds = clear.getBounds(); int n1 = v.getRight(); int n2 = v.getRight()+rBounds.width(); int n3 = v.getPaddingTop(); int n4 = v.getHeight()-v.getPaddingBottom(); if(x>=(n1) && x<=(n2) && y>=n3 && y<=(n4)) { uIDEditText.setText(""); event.setAction(MotionEvent.ACTION_CANCEL); } } break; } }
解决方法
我解决了它……创建了以下代码
public class CustomEditText extends EditText { private Drawable dRight; private Rect rBounds; CustomEditText(Context context,AttributeSet attributeSet){ super(context,attributeSet); } @Override public void setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom) { /*if (left != null) { dLeft = left; }*/ if (right != null) { dRight = right; } super.setCompoundDrawables(left,top,right,bottom); } @Override public void addTextChangedListener(TextWatcher watcher) { super.addTextChangedListener(watcher); } @Override protected void onTextChanged(CharSequence text,int start,int lengthBefore,int lengthAfter) { super.onTextChanged(text,start,lengthBefore,lengthAfter); if(this.getText().toString().length()>0) this.setCompoundDrawablesWithIntrinsicBounds(null,dRight,null); else this.setCompoundDrawablesWithIntrinsicBounds(null,null); } @Override protected void finalize() throws Throwable { dRight = null; rBounds = null; super.finalize(); } }
并添加到xml中:
<com.example.screen.CustomEditText android:id="@+id/uIDEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableRight="@drawable/round_clear" android:textColor="#ffffff" />
在Activity中(edittext ontouch侦听器):
uIDEditText.setonTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v,MotionEvent event) { if (event.getAction() != MotionEvent.ACTION_UP) return false; if (event.getX() > uIDEditText.getWidth() - clear.getIntrinsicWidth()) { uIDEditText.setText(""); event.setAction(MotionEvent.ACTION_CANCEL); } return false; } });
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。