How can I add an image on EditText

2019-01-03 01:39发布

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

if anyone knows please give sample code for that.

10条回答
Explosion°爆炸
2楼-- · 2019-01-03 02:35

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

查看更多
我命由我不由天
3楼-- · 2019-01-03 02:38

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);
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-03 02:42

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);
查看更多
再贱就再见
5楼-- · 2019-01-03 02:44

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);
查看更多
登录 后发表回答