EditText常见问题汇总

版权声明:欢迎转载,转载请注明出处 https://blog.csdn.net/u012810020/article/details/53260368

简述:

    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();
            }
        });

猜你喜欢

转载自blog.csdn.net/u012810020/article/details/53260368