java - 如何在编辑文本中添加可触摸的 ImageView ?

标签 java android android-layout android-widget

我需要在像 ms word 屏幕这样的编辑文本中添加一个可触摸的 ImageView 。我们如何为此目的设计一个 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]);
        }   

}

有知道的请帮帮我谢谢

最佳答案

我就是这样做的。 Drawable 将位于 EditText 的右侧。请尝试代码。

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

关于java - 如何在编辑文本中添加可触摸的 ImageView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13453553/

相关文章:

java - 如何通过 java 驱动程序通过聚合函数(如 group、count 和 distinct)访问 mongodb 集合?

java - java能识别带小数的负0吗?

java - 使用 GitHub 通过 Jenkins 将 EAR 部署到 WAS8.5 时出现 IncompatibleClassChangeError

java - 关于android中的 Activity 上下文和处理程序

android - 如何以编程方式在 NestedScrollView 中显示滚动条。

java - 从多个 JTextField 检索和分析数据

android - 为什么删除 rawQuery 需要 moveToFirst 才能实际删除行?

android - 如何使用 Ant 构建 Android 示例项目? build.xml 不存在

android - 动画相对布局以像 Snack Bar 一样向下/向上移动

Android TabLayout如何制作两条下划线