首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android中的EditText中单击监听器进行绘图?

在Android中的EditText中单击监听器进行绘图?
EN

Stack Overflow用户
提问于 2015-11-11 20:29:24
回答 2查看 11K关注 0票数 4

是否可以在设计器中声明为drawableleft的可绘制文件上添加OnClickListener

代码语言:javascript
复制
android:drawableLeft="@drawable/ic_password"

或者有没有办法在该EditText的左侧添加一个ImageView

EN

回答 2

Stack Overflow用户

发布于 2016-04-28 19:31:12

用于绘图的单击监听器:

代码语言:javascript
复制
 editText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final int DRAWABLE_LEFT = 0;
        final int DRAWABLE_TOP = 1;
        final int DRAWABLE_RIGHT = 2;
        final int DRAWABLE_BOTTOM = 3;

       if(event.getRawX() <= (editText.getCompoundDrawables()[DRAWABLE_LEFT].getBounds().width()))
        {
              // your action here
             return true;
        }
        return false;
    }
});

如果要单击drawable right监听器,请使用以下条件

代码语言:javascript
复制
if(event.getAction() == MotionEvent.ACTION_UP) {
            if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) 
票数 10
EN

Stack Overflow用户

发布于 2015-11-11 20:44:41

你可以通过一个RelativeLayout来实现:

代码语言:javascript
复制
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp">

    <ImageView
        android:id="@+id/image_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_iamge"/>

    <EditText
        android:id="@+id/text_id"
        android:layout_toRightOf="@id/image_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="16dp"
        android:text="Your text" />

 </RelativeLayout>

并将onClickListener添加到代码中的ImageView中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33650575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档