2010-09-13 113 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