2010-09-13 110 views

回答

61

如果是这样的:

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

java java逗号nd应该调用setBounds(Rect),否则它会出现图像 – pengwang 2010-09-15 08:35:54

+0

http://mgmblog.com/2010/06/04/setcompounddrawable-to-add-drawables-to-textview/ – 2011-05-27 17:12:38

+0

您已经使用了drawable中的图像显示,但是,如果有可能将图库从图库中选取到编辑文本视图中,我需要显示图像扔图库图像。 – Manoj 2014-05-27 11:03:21

7

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

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

+0

@Thanks sami,我想动态添加图像在编辑文本中, – sivaraj 2010-09-14 08:23:53

24

你也可以试试这个

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

+1如果您想要动态添加多个可绘制的编辑文本,此代码很有用。例如chating输入edittext。用户可以选择笑脸图标。需要将此Spanable String附加到可编辑。 – Javanator 2013-06-05 10:21:31

+0

'setSpan'中的'3'参数来自哪里? – akhyar 2014-11-18 07:49:14

+0

这是字符串“abc”的长度 – 2014-11-18 07:53:16

38

使用android:drawableRightandroid: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" 
     /> 
+1

这个人应该得到一枚奖章。多谢,伙计! – 2016-01-28 22:01:17

+0

在最新的android studio 2.2.3中,建议使用android:drawableEnd代替android:drawableRight – 2017-01-13 09:50:20

59

enter image description here

使用框架布局是很容易克服这一点。另一个好处是,你可以给按钮的点击事件。如果你设置图标使用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> 
+0

如何将点击事件添加到按钮? – Scorpion 2013-02-15 11:41:08

+0

findViewById(R.id.delete).setOnClickListener(new View。OnClickListener(){ \t \t \t \t \t \t @Override \t \t \t公共无效的onClick(视图v){ \t \t \t \t // TODO自动生成方法存根 \t \t \t \t \t \t \t} \t \t}); – 2013-02-15 12:28:42

+0

你能提供可绘制的图像! – ManishSB 2014-08-21 18:24:44

1

扩展的EditText类,做代码为u希望它是

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); 
} 
+0

imageResource是什么?它会像/sdcard/images/image1.jpg那样的图像路径吗? – NarendraJi 2015-11-24 10:20:53

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

创建的EditText

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

然后创建该图像的绘制实例

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

OR

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