Как программно установить drawableRight на Android Edittext?

Я знаю о set drawableRight в XML. но я должен был сделать это программно, потому что это изменение в соответствии с некоторым условием.

9 ответов


вы можете использовать функцию ниже:

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

порядок параметров, соответствующий местоположению drawable: left, top, right, bottom


Найти здесь

EditText myEdit = (EditText) findViewById(R.id.myEdit);
myEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon, 0);  
// where params are (left,top,right,bottom)

вы можете также установить drawable прокладку программно:

myEdit.setCompoundDrawablePadding("Padding value");

попробовать, как показано ниже:

Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
EdtText.setCompoundDrawablesWithIntrinsicBounds( 0, 0, img, 0);

Edit:

 int img = R.drawable.smiley;
 EdtText.setCompoundDrawablesWithIntrinsicBounds( 0, 0, img, 0);

int img = R.drawable.smiley;
editText.setCompoundDrawables( null, null, img, null );

объяснил здесь

setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)

устанавливает Drawables (если таковые имеются), чтобы появиться слева, выше, справа и ниже текста. Используйте 0, если вы не хотите Drawable там. Границы Drawables будут установлены на их внутренние границы.


попробуй:

EditText editFirstname = (EditText) findViewById(R.id.edit_fname);
Drawable icUser = getResources().getDrawable(R.drawable.ic_user);
editFirstname.setCompoundDrawablesWithIntrinsicBounds(null, null, icUser, null);

затем вы можете добавить прослушиватель касания к этому конкретному drawable.


вы можете использовать свой вид editText (здесь это txview), встроенный в функцию setCompoundDrawablesWithIntrinsicbounds (), чтобы сделать то, что вы ищете

в моем коде я использовал его так . txview.setCompoundDrawablesWithIntrinsicbounds (0,0, R. drawable.ic_arrow_drop_up,0);

txview.setCompoundDrawablesWithIntrinsicBounds(left,top,right,bottom);

        et_feedback.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {

                }
              et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.feedback_new, 0, 0);                
               et_feedback.setTextColor(Color.BLACK);

            }
        });

скрыть Drawable с помощью этого

et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);

для изменения влево и вправо и одновременно я использую эту одну строку.

download.setCompoundDrawablesWithIntrinsicBounds( R.drawable.ic_lock_open_white_24dp, 0, R.drawable.ic_lock_open_white_24dp, 0);

Если это требует Android графики drawable, то это будет работать

Drawable dw = getApplicationContext().getResources().getDrawable(R.drawable.edit);
Button start = (Button)findViewById(R.id.buttonStart);
start.setCompoundDrawablesWithIntrinsicBounds(dw, null, null, null);