如何在EditText上添加图像

时间:2022-02-08 07:26:28

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

我想在EditText中动态添加图像。可能吗?

if anyone knows please give sample code for that.

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

10 个解决方案

#1


62  

If something like this:

如果是这样的话:

如何在EditText上添加图像

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.

就是你在说什么,然后你只需要设置Drawable {Right |左|顶部|在xml中使用Bottom}属性,或者调用相应的java命令。

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

#2


59  

如何在EditText上添加图像

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.

使用框架布局很容易克服这个问题。另一个优点是你可以为按钮提供点击事件。如果你使用setCompoundDrawables设置图标,你就不能给出点击事件。我在我的项目中实现了搜索栏有删除和搜索图标。

<FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/search"
                android:layout_width="fill_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>

#3


43  

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

使用android:drawableRight或android:drawableLeft(取决于你喜欢对齐图像的位置)

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

#4


24  

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

#5


16  

如何在EditText上添加图像

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

您可以使用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);

#6


7  

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

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

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".

如果你想通过九个补丁修​​改样式,或者如果你想在EditText的左边添加一个小图像,可以考虑使用android:drawableRight =“@ drawable / icon”。

#7


1  

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

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

#8


1  

You can use:

您可以使用:

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

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

如下所示:调用setCompoundDrawables()不会显示Compound Drawable

#9


0  

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

#10


0  

Create an instance of the EditText

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

然后通过调用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

上面的行将图像设置在EditText的右侧

#1


62  

If something like this:

如果是这样的话:

如何在EditText上添加图像

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.

就是你在说什么,然后你只需要设置Drawable {Right |左|顶部|在xml中使用Bottom}属性,或者调用相应的java命令。

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

#2


59  

如何在EditText上添加图像

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.

使用框架布局很容易克服这个问题。另一个优点是你可以为按钮提供点击事件。如果你使用setCompoundDrawables设置图标,你就不能给出点击事件。我在我的项目中实现了搜索栏有删除和搜索图标。

<FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/search"
                android:layout_width="fill_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>

#3


43  

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

使用android:drawableRight或android:drawableLeft(取决于你喜欢对齐图像的位置)

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

#4


24  

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

#5


16  

如何在EditText上添加图像

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

您可以使用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);

#6


7  

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

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

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".

如果你想通过九个补丁修​​改样式,或者如果你想在EditText的左边添加一个小图像,可以考虑使用android:drawableRight =“@ drawable / icon”。

#7


1  

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

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

#8


1  

You can use:

您可以使用:

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

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

如下所示:调用setCompoundDrawables()不会显示Compound Drawable

#9


0  

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

#10


0  

Create an instance of the EditText

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

然后通过调用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

上面的行将图像设置在EditText的右侧