Подтвердить что ты не робот

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

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

4b9b3361

Ответ 1

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

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

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

Ответ 2

Найти далее здесь

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

Вы также можете программно программировать обратную прокладку:

myEdit.setCompoundDrawablePadding("Padding value");

Ответ 3

Попробуйте сделать следующее:

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

Изменить:

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

Ответ 4

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

Объяснено здесь

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

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

Ответ 5

Try:

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

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

Ответ 6

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

Ответ 7

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

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

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