在EditText中插入多个图像

时间:2018-10-03 01:00:32

标签: android android-studio

我正在为聋哑人士开发应用程序,因此为该应用程序创建了专用键盘。现在,我想在EditText中设置图像,这些图像是聋哑人使用的符号,我想要这样的东西: deaf-muet keyboard + EditText

在那个editText中,我只能设置第一个符号,它表示第一个字母,但是我想为每个字母设置许多图像。 我使用了.setCompoundDrawablesWithIntrinsicBounds(),但它只给了我第一个字母。 如何在EditText或ViewText中设置多个图像?

2 个答案:

答案 0 :(得分:0)

尝试一下,希望对您有帮助

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/icon">
</EditText>

您可以尝试的同一件事:

android:drawableRight
android:drawableTop
android:drawableBottom
android:drawablePadding

答案 1 :(得分:0)

此类是自定义editText,可用于在其中放置可绘制对象。

public class WithIconEditText extends android.support.v7.widget.AppCompatEditText {
int actionX, actionY;
private Drawable drawableRight;
private Drawable drawableLeft;
private Drawable drawableTop;
private Drawable drawableBottom;
private DrawableClickListener clickListener;

public WithIconEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public WithIconEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
    if (left != null)
        drawableLeft = left;
    if (right != null)
        drawableRight = right;
    if (top != null)
        drawableTop = top;
    if (bottom != null)
        drawableBottom = bottom;
    super.setCompoundDrawables(left, top, right, bottom);
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
    Rect bounds;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        actionX = (int) event.getX();
        actionY = (int) event.getY();
        if (drawableBottom != null && drawableBottom.getBounds().contains(actionX, actionY)) {
            clickListener.onClick(DrawableClickListener.DrawablePosition.BOTTOM);
            return super.onTouchEvent(event);
        }
        if (drawableTop != null && drawableTop.getBounds().contains(actionX, actionY)) {
            clickListener.onClick(DrawableClickListener.DrawablePosition.TOP);
            return super.onTouchEvent(event);
        }
        if (drawableLeft != null) {
            bounds = drawableLeft.getBounds();
            int x, y;
            int extraTapArea = (int) (13 * getResources().getDisplayMetrics().density + 0.5);
            x = actionX;
            y = actionY;
            if (!bounds.contains(actionX, actionY)) {
                x = actionX - extraTapArea;
                y = actionY - extraTapArea;
                if (x <= 0)
                    x = actionX;
                if (y <= 0)
                    y = actionY;
                if (x < y)
                    y = x;
            }
            if (bounds.contains(x, y) && clickListener != null) {
                clickListener.onClick(DrawableClickListener.DrawablePosition.LEFT);
                event.setAction(MotionEvent.ACTION_CANCEL);
                return false;
            }
        }
        if (drawableRight != null) {
            bounds = drawableRight.getBounds();
            int x, y;
            int extraTapArea = 13;
            x = actionX + extraTapArea;
            y = actionY - extraTapArea;
            x = getWidth() - x;
            if (x <= 0) {
                x += extraTapArea;
            }
            if (y <= 0)
                y = actionY;
            if (bounds.contains(x, y) && clickListener != null) {
                clickListener.onClick(DrawableClickListener.DrawablePosition.RIGHT);
                event.setAction(MotionEvent.ACTION_CANCEL);
                return false;
            }
            return super.onTouchEvent(event);
        }
    }
    return super.onTouchEvent(event);
}

@Override
protected void finalize() throws Throwable {
    drawableRight = null;
    drawableBottom = null;
    drawableLeft = null;
    drawableTop = null;
    super.finalize();
}

public void setDrawableClickListener(DrawableClickListener listener) {
    this.clickListener = listener;
}

public interface DrawableClickListener {
    void onClick(DrawablePosition target);

    enum DrawablePosition {TOP, BOTTOM, LEFT, RIGHT}
}
}