如何在 EditText 上添加图像

发布于 2024-09-18 13:45:50 字数 69 浏览 8 评论 0原文

我想在 EditText 中动态添加图像。是否可以?

如果有人知道请给出示例代码。

I want to dynamically add image in EditText. Is it possible?

if anyone knows please give sample code for that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

彡翼 2024-09-25 13:45:50

如果您正在谈论的是这样的内容:

EditCheck

,那么您只需设置 Drawable {对|左|顶部 | xml中的Bottom}属性,或者调用相应的java命令。

EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);

If something like this:

EditCheck

is what you're talking about, then you just need to either set the Drawable{Right | Left | Top | Bottom} property in the xml, or call the corresponding java command.

EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);
夜血缘 2024-09-25 13:45:50

在此处输入图像描述

使用框架布局很容易克服这个问题。这里的另一个优点是你可以给出点击事件对于按钮。如果您使用 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>

enter image description here

Using the frame layout it is very easy to overcome this.Here another advantage is that you can give click events for buttons.if you set icons using setCompoundDrawables, you cant give the click events.I have implemented in my project that search bar has delete and search icons.

<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>
少年亿悲伤 2024-09-25 13:45:50

使用 android:drawableRight 或 android: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"
        />  

using android:drawableRight or android:drawableLeft
(depend where you prefer align image)

<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"
        />  
_失温 2024-09-25 13:45:50

你也可以试试这个

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

you can also try this

    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);
清旖 2024-09-25 13:45:50

测试图片

您可以使用 setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int Bottom)

    EditText text = (EditText) findViewById(R.id.edtTest);
    text.setText("Test");
    text.setCompoundDrawablesWithIntrinsicBounds(null, null,
                       getResources().getDrawable(R.drawable.myDrawable, null), null);

Test Image

You can use setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom).

    EditText text = (EditText) findViewById(R.id.edtTest);
    text.setText("Test");
    text.setCompoundDrawablesWithIntrinsicBounds(null, null,
                       getResources().getDrawable(R.drawable.myDrawable, null), null);
爱她像谁 2024-09-25 13:45:50

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

如果您想使用九个补丁或其他方式修改样式,但如果您想在 EditText 左侧添加小图像,请考虑使用 android:drawableRight="@drawable/icon “

You can add an image to your EditText through android:background="@drawable/img".

If you want to modify the style by using nine patch or else, but if you want to add a small image in the left of your EditText consider using android:drawableRight="@drawable/icon".

花心好男孩 2024-09-25 13:45:50

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

extend the edittext class and do code as u want it to be

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);
}
终止放荡 2024-09-25 13:45:50

您可以使用:

editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.pic_name, 0); 

按照此处的建议:
调用 setCompoundDrawables() 不会显示复合 Drawable

You can use:

editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.pic_name, 0); 

as suggested here:
Calling setCompoundDrawables() doesn't display the Compound Drawable

一身仙ぐ女味 2024-09-25 13:45:50

创建 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 的右侧设置图像

Create an instance of the EditText

EditText editText = (EditText) findViewById(R.id.EditText1);

Then create a drawable instance of the image

Drawable image = getResources().getDrawable(R.drawable.ic_warning); // API 21 and below.

OR

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

Then set the image by calling setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);

editText.setCompoundDrawablesWithIntrinsicBounds(null, null, image, null); 

The above line will set the image in the right side of the EditText

爺獨霸怡葒院 2024-09-25 13:45:50

如果您属于适配器类,则可以尝试此操作

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

并且
如果您参加活动课,可以尝试此操作

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

You can try this if you are in adapter class

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

and
You can try this if you are in activity class

    myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null,
            getResources().getDrawable(R.drawable.ic_launcher),
            null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文