如何在EditText上添加图片

50

我希望能够在EditText中动态添加图像。这是否可能?

如果有人知道,请提供相应的示例代码。


3
定义“在EditText中添加图像”: - CommonsWare
10个回答

64
如果你说的是这样一种情况:

EditCheck

,那么你只需要在XML文件中设置Drawable{Right | Left | Top | Bottom}属性,或调用相应的Java命令即可。
EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);

java命令应该调用setBounds(Rect)方法,否则图片将无法显示。 - pengwang
你已经使用了drawable中的图片,但我需要从相册中选择图片并显示在编辑文本视图中。如果可能的话,请从相册中选择图片。 - Manoj
只需获取图像并将其放入BitmapDrawable中即可。 - CaseyB
1
好的,我都做了,但图片在编辑文本框中没有显示。 - Manoj
你之后使它无效了吗? - CaseyB
请使用ContextCompat.getDrawable(getContext(),R.drawable.check_box)代替getResources().getDrawable(R.drawable.check_box) - Harish Gyanani

60

enter image description here

使用帧布局可以很容易地解决这个问题。另一个优点是您可以为按钮设置点击事件。如果您使用setCompoundDrawables设置图标,则无法设置点击事件。我已在我的项目中实现了搜索栏具有删除和搜索图标的功能。
<FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/search_bar"
                android:drawablePadding="8dp"
                android:paddingLeft="30dp"
                android:paddingRight="10dp"
                android:inputType="text"
                android:maxLines="1">

                <requestFocus />
            </EditText>

            <Button
                android:id="@+id/searchBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/icon_magnify" />

            <Button
                android:id="@+id/delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:layout_margin="8dp"
                android:background="@drawable/icon_remove" />
        </FrameLayout>

如何为按钮添加点击事件? - Scorpion
findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); - Ramesh Akula
你能提供可绘制的图像吗? - ManishSB
使用以下代码android:inputType="text" android:maxLines="1",并从EditText中删除singleLine - Harish Gyanani

53

使用 android:drawableRightandroid:drawableLeft (取决于您喜欢对齐图像的位置)


<EditText 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/searchedTxt"
        android:width="200dp"
        android:layout_alignParentRight="true"
        android:drawableRight="@android:drawable/ic_menu_search"
        android:drawablePadding="@dimen/dimen_10dp"
        />  

2
这个人值得一枚奖章。谢谢,老兄! - Gabriel Lidenor
1
在最新的Android Studio 2.2.3中,建议使用android:drawableEnd而不是android:drawableRight。 - Babajide Apata

28

你也可以尝试这个

    SpannableString ss = new SpannableString("abc\n");
    Drawable d = img.getDrawable();
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    editT.setText(ss);

1
如果您想动态添加多个可绘制对象到EditText中,那么这段代码非常有用,例如聊天输入框。用户可以选择表情符号图标,需要将此Spanable字符串附加到可编辑的文本中。 - Rohit Sharma
setSpan 中的 3 参数从哪里来? - akhy
"abc"字符串的长度是多少? - Buda Gavril
嗨,在我的情况下,它看起来像这样链接。有什么想法吗?我是这样下载可绘制对象的:thiss.getResources().getDrawable(R.drawable.friends) - Eliasz Kubala

16

7

您可以通过android:background="@drawable/img"为您的EditText添加图像。

如果您想使用九宫格或其他方式修改样式,但是如果您想在左侧添加小图像,则考虑使用android:drawableRight="@drawable/icon"


3
扩展EditText类并按照您想要的方式编写代码。
public void setImage(String imageResource) {
    int position = Selection.getSelectionStart(MyEditText.this
            .getText());

    Spanned e = Html.fromHtml("<img src=\"" + imageResource + "\">",
            imageGetter, null);

    MyEditText.this.getText().insert(position, e);
}

imageResource会是什么?它会是像/sdcard/images/image1.jpg这样的图片路径吗? - Narendra Singh

1

1
创建一个EditText实例。
EditText editText = (EditText) findViewById(R.id.EditText1);

然后创建图像的可绘制实例。
Drawable image = getResources().getDrawable(R.drawable.ic_warning); // API 21 and below.

或者

Drawable image = getDrawable(R.drawable.ic_warning); // API 22 and above.

然后通过调用setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);来设置图像。
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, image, null); 

上述代码将把图片设置在右侧的EditText中。

0

如果你在适配器类中,可以尝试这个方法

    holder.myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
            _context.getResources().getDrawable(R.drawable.ic_launcher),
            null);

并且 如果您在活动类中,可以尝试这个

    myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
            getResources().getDrawable(R.drawable.ic_launcher),
            null);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接