EditText常见问题汇总
Posted lzy_tinyjoy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EditText常见问题汇总相关的知识,希望对你有一定的参考价值。
简述:
android EditText常见的一写处理方法汇总,这里会不断更新。
EditView默认不显示软键盘
方案一:在 AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为 adjustUnspecified|stateHidden:
<activity
android:name=".trace.view.SupplementReceiptActivity"
android:exported="false"
android:windowSoftInputMode="adjustUnspecified|stateHidden" />
这种方案只是让键盘不显示,并不能让Edittext失去焦点。
方案二:让 EditText失去焦点,使用EditText的clearFocus方法
EditText edit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
EditText设置DrawableRight和DrawableLeft 点击事件
在开发中经常会遇到这样的情况:点击搜索图标开始搜索输入框内容,OK,那就得重写EditText的onTouch()方法:
public class YCEditText extends EditText
private DrawableLeftListener mLeftListener;
private DrawableRightListener mRightListener;
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
public YCEditText(Context context, AttributeSet attrs)
super(context, attrs);
public YCEditText(Context context, AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
public YCEditText(Context context)
super(context);
public void setDrawableLeftListener(DrawableLeftListener listener)
this.mLeftListener = listener;
public void setDrawableRightListener(DrawableRightListener listener)
this.mRightListener = listener;
public interface DrawableLeftListener
public void onDrawableLeftClick(View view);
public interface DrawableRightListener
public void onDrawableRightClick(View view);
@Override
public boolean onTouchEvent(MotionEvent event)
switch (event.getAction())
case MotionEvent.ACTION_UP:
if (mRightListener != null)
Drawable drawableRight = getCompoundDrawables()[DRAWABLE_RIGHT];
if (drawableRight != null && event.getRawX() >= (getRight() - drawableRight.getBounds().width()))
mRightListener.onDrawableRightClick(this);
return true;
if (mLeftListener != null)
Drawable drawableLeft = getCompoundDrawables()[DRAWABLE_LEFT];
if (drawableLeft != null && event.getRawX() <= (getLeft() + drawableLeft.getBounds().width()))
mLeftListener.onDrawableLeftClick(this);
return true;
break;
return super.onTouchEvent(event);
使用就像一个button一样,如下:
ycEditText = (YCEditText) findViewById(R.id.xedit);
ycEditText.setDrawableLeftListener(new YCEditText.DrawableLeftListener()
@Override
public void onDrawableLeftClick(View view)
Toast.makeText(MainActivity.this, XView.getText().toString(), Toast.LENGTH_SHORT).show();
);
以上是关于EditText常见问题汇总的主要内容,如果未能解决你的问题,请参考以下文章
安卓基础ImageView与EditText联动实现隐藏与显示密码