在我的 Activity 应用程序中,我想在EditText
单击内部(焦点)EditText 并键入一个键清除时进行设置,该清除button
应出现在右侧,EditText
当 EditText 为空时,必须删除该清除按钮。
但它没有向我展示..
我应该在这里实施哪个事件..?onTouch
或onFocusChange
或addTextChangedListener
还有什么代码在那里..?以下代码我在活动中完成...
在活动中:
clear = getResources().getDrawable(R.drawable.round_clear);
clear.setBounds(0, 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, null, null);
break;
case R.id.pwdEditText:
if(hasFocus && !pwdEditText.getText().toString().isEmpty())
pwdEditText.setCompoundDrawables(null, null, clear, null);
else
pwdEditText.setCompoundDrawables(null, null, 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;
}
}