50 votes

Comment puis-je ajouter une image sur EditText

Je veux ajouter dynamiquement des images en EditText. Est-il possible?

si quelqu'un sait merci de donner un exemple de code pour que.

64voto

CaseyB Points 16014

Si quelque chose comme ceci:

EditCheck

est ce que vous êtes en train de parler, puis il vous suffit de définir l' Drawable{Right | Left | Top | Bottom} propriété dans le fichier xml, ou appeler le correspondant de commande java.

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

60voto

Ramesh Points 3483

enter image description here

À l'aide de la structure du châssis, il est très facile de remédier à cela.Ici un autre avantage est que vous pouvez fournir sur les événements pour les boutons.si vous définissez les icônes à l'aide setCompoundDrawables, vous ne pouvez pas donner le cliquez sur événements.J'ai mis en œuvre de mon projet que la barre de recherche a supprimer et rechercher des icônes.

<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:singleLine="true" >
                <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>

53voto

Ging3r Points 564

à l'aide de android:drawableRight ou android:drawableLeft (dépend où vous préférez aligner image)

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

28voto

Buda Gavril Points 4594

vous pouvez également essayer cette

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

16voto

Mihir Points 1339

Test Image

Vous pouvez utiliser setCompoundDrawablesWithIntrinsicbounds (int gauche, int haut, int droite, int bas).

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

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X