Java 如何在编辑文本中添加可触摸图像视图?

Java 如何在编辑文本中添加可触摸图像视图?,java,android,android-layout,android-widget,Java,Android,Android Layout,Android Widget,我需要在编辑文本(如ms word屏幕)中添加一个可触摸图像视图。我们如何为此设计android布局屏幕?我已经尝试了以下代码: public class edittext extends EditText { public String defaultValue = ""; final Drawable imgX = getResources().getDrawable(android.R.drawable.presence_offline ); // X imag

我需要在编辑文本(如ms word屏幕)中添加一个可触摸图像视图。我们如何为此设计android布局屏幕?我已经尝试了以下代码:

public class edittext extends EditText 
{
     public String defaultValue = "";
        final Drawable imgX = getResources().getDrawable(android.R.drawable.presence_offline ); // X image

    private Html.ImageGetter imageGetter;
    public edittext(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
        // TODO Auto-generated constructor stub
    }
    public edittext(Context context, AttributeSet attrs, int defStyle) 
    {
        super(context, attrs, defStyle);
        init();
        // TODO Auto-generated constructor stub
    }
    public edittext(Context context) 
    {
        super(context);
        init();
        // TODO Auto-generated constructor stub
    }
     void init()  {

            // Set bounds of our X button
            imgX.setBounds(0, 0, imgX.getIntrinsicWidth(), imgX.getIntrinsicHeight());      

            // There may be initial text in the field, so we may need to display the button
            manageClearButton();

           edittext.this.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {

                    edittext et = edittext.this;

                    // Is there an X showing?
                    if (et.getCompoundDrawables()[2] == null) return false;
                    // Only do this for up touches
                    if (event.getAction() != MotionEvent.ACTION_UP) return false;
                    // Is touch on our clear button?
                    if (event.getX() > et.getWidth() - et.getPaddingRight() - imgX.getIntrinsicWidth()) {
                        et.setText("");
                        edittext.this.removeClearButton();
                    }
                    return false;
                }
            });

            edittext.this.addTextChangedListener(new TextWatcher() {
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                   edittext.this.manageClearButton();
                }

                public void afterTextChanged(Editable arg0) {
                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
            });
        }

        void manageClearButton() {
            if (this.getText().toString().equals("") )
                removeClearButton();
            else
                addClearButton();
        }
        void addClearButton() {
            this.setCompoundDrawables(this.getCompoundDrawables()[0], 
                    this.getCompoundDrawables()[1],
                    imgX,
                    this.getCompoundDrawables()[3]);
        }
        void removeClearButton() {
            this.setCompoundDrawables(this.getCompoundDrawables()[0], 
                    this.getCompoundDrawables()[1],
                    null,
                    this.getCompoundDrawables()[3]);
        }   

}

如果有人知道,请帮我谢谢。

我就是这样做的。可绘制文本将位于编辑文本的右侧。请尝试代码

EditText contactLine = new EditText(getActivity());
Drawable drawable =  getActivity().getResources().getDrawable(...);
drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));
contactLine.setCompoundDrawables(null, null, drawable, null);
contactLine.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Drawable co =  v.getCompoundDrawables()[2];
            if (co == null) {
                return false;
            }
            if (event.getAction() != MotionEvent.ACTION_DOWN) {
                return false;
            }
            if (event.getX() > v.getMeasuredWidth() - v.getPaddingRight()
                    - co.getIntrinsicWidth()) {
                whatYouWantToDo();
                return true;
            } else {
                return false;
            }
        }
    });

我就是这样做的。可绘制文本将位于编辑文本的右侧。请尝试代码

EditText contactLine = new EditText(getActivity());
Drawable drawable =  getActivity().getResources().getDrawable(...);
drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));
contactLine.setCompoundDrawables(null, null, drawable, null);
contactLine.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Drawable co =  v.getCompoundDrawables()[2];
            if (co == null) {
                return false;
            }
            if (event.getAction() != MotionEvent.ACTION_DOWN) {
                return false;
            }
            if (event.getX() > v.getMeasuredWidth() - v.getPaddingRight()
                    - co.getIntrinsicWidth()) {
                whatYouWantToDo();
                return true;
            } else {
                return false;
            }
        }
    });

可能您可以在布局中使用RelativeLayout实现这一点

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/et1"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:clickable="false"
        android:focusable="false"
        android:background="@drawable/ic_action_search"/>
    <ImageView 
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et1"
        android:paddingLeft="5dp"
        android:background="@drawable/ic_launcher"
        />

</RelativeLayout>

可能您可以在使用RelativeLayout的布局中执行此操作

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/et1"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:clickable="false"
        android:focusable="false"
        android:background="@drawable/ic_action_search"/>
    <ImageView 
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et1"
        android:paddingLeft="5dp"
        android:background="@drawable/ic_launcher"
        />

</RelativeLayout>

对于相对视图中的可移动视图,您可以这样做。DragView可以是ImageView

     View DragView;

     private boolean inDrag;
     int xDragTouchOffset, yDragTouchOffset;
     @Override
    public boolean onTouchEvent(View View, MotionEvent event) {
        final int action = event.getAction();
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        boolean result = false;
        if (action == MotionEvent.ACTION_DOWN)
            inDrag = true;
            xDragTouchOffset = x;
            yDragTouchOffset = y;
            result = true;
        } else if (action == MotionEvent.ACTION_MOVE && inDrag == true) {
            setDragImagePosition(x, y);//HERE YOU HANDLE THE POSITION OF YOUR VIEW
            result = true;
        } else if (action == MotionEvent.ACTION_UP && inDrag ==true) {

            inDrag = false;
            result = true;
        }

        return result;

    }

    private void setDragImagePosition(int x, int y){

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) DragView
                .getLayoutParams();

        lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
                - yDragImageOffset - yDragTouchOffset, 0, 0);
        dragImage.setLayoutParams(lp);
     }

对于相对视图中的可移动视图,可以这样做。DragView可以是ImageView

     View DragView;

     private boolean inDrag;
     int xDragTouchOffset, yDragTouchOffset;
     @Override
    public boolean onTouchEvent(View View, MotionEvent event) {
        final int action = event.getAction();
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        boolean result = false;
        if (action == MotionEvent.ACTION_DOWN)
            inDrag = true;
            xDragTouchOffset = x;
            yDragTouchOffset = y;
            result = true;
        } else if (action == MotionEvent.ACTION_MOVE && inDrag == true) {
            setDragImagePosition(x, y);//HERE YOU HANDLE THE POSITION OF YOUR VIEW
            result = true;
        } else if (action == MotionEvent.ACTION_UP && inDrag ==true) {

            inDrag = false;
            result = true;
        }

        return result;

    }

    private void setDragImagePosition(int x, int y){

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) DragView
                .getLayoutParams();

        lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
                - yDragImageOffset - yDragTouchOffset, 0, 0);
        dragImage.setLayoutParams(lp);
     }
或者您可以简单地使用,例如:

public void addListenerOnImageButton() {
            imageButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // called when imageButton Clicked
                }

            });}
或者您可以简单地使用,例如:

public void addListenerOnImageButton() {
            imageButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // called when imageButton Clicked
                }

            });}

谢谢你的回答,但是我如何才能使我的可绘制对象在我的edittext内移动。这可能吗?我只需要移动我的可绘制对象。请帮助我你可以使用相对布局或绝对布局来保存edittext和imageView。然后您必须实现View.Touchlistener。在这里,您可以获得此侦听器中的位置信息,并将位置更改添加到您的视图中。谢谢您的回答,但是我如何才能使我的可绘制对象在我的edittext中移动。这可能吗?我只需要移动我的可绘制对象。请帮助我您可以使用RelativeLayout或AbsoluteLayout来保存edittext和imageView。然后您必须实现View.Touchlistener。在这里,您可以在此侦听器中获取位置信息,并将位置更改添加到您的视图中。感谢您的帮助,这似乎是错误的。我只需要将我的图像移动到我的编辑文本中。请在相对视图中给出最佳提示。您可以在另一个视图上方查看图像。因此,创建一个layout.xml,其中包含edittext(完整的父项)和imageview。然后你可以设置图像视图的位置。谢谢你,也许你…我正试着用你的方式…让我看看…无论如何谢谢你的宝贵提示…谢谢你的帮助,这似乎是错误的。我只需要在我的编辑文本中移动我的图像。请给一个相对的提示你可以在另一个视图之上有一个视图。因此,创建一个layout.xml,其中包含edittext(完整的父项)和imageview。然后你可以设置imageview的位置。谢谢你,也许你…我正试着用你的方式…让我看看…无论如何谢谢你宝贵的提示。。。