简体   繁体   English

如何在 Android Edittext 上以编程方式设置 drawableRight?

[英]How to programmatically set drawableRight on Android Edittext?

I know about set drawableRight in XML.我知道在 XML 中设置 drawableRight。 but i required to do it programmatically because it is change as per some condition.但我需要以编程方式进行,因为它会根据某些条件进行更改。

You can use the function below:您可以使用以下功能:

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

or (if you want to pass the drawable itself instead of its ID)或者(如果你想传递drawable本身而不是它的ID)

editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context,R.drawable.drawableRight), null)

The order of params corresponding to the drawable location is: left, top, right, bottom drawable位置对应的params顺序为:左、上、右、下

Find Further here 在这里找到更多

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

You can also set drawable padding programmatically:您还可以以编程方式设置可绘制填充:

myEdit.setCompoundDrawablePadding("Padding value");

Try like below:尝试如下:

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

Try:尝试:

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

Then you can add a touch listener to that specific drawable.然后,您可以向该特定可绘制对象添加触摸侦听器。

为了一次改变左右,我使用这条线。

download.setCompoundDrawablesWithIntrinsicBounds( R.drawable.ic_lock_open_white_24dp, 0, R.drawable.ic_lock_open_white_24dp, 0);
int img = R.drawable.smiley;
editText.setCompoundDrawables( null, null, img, null );

Explained here 在这里解释

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

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text.将 Drawables(如果有)设置为出现在文本的左侧、上方、右侧和下方。 Use 0 if you do not want a Drawable there.如果您不想在那里使用 Drawable,请使用 0。 The Drawables' bounds will be set to their intrinsic bounds. Drawables 的边界将设置为它们的内在边界。

If it requires android graphics drawable then this will work如果它需要可绘制的 android 图形,那么这将起作用

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

You can use your editText view (here it is txview) built in function setCompoundDrawablesWithIntrinsicBounds() to do what you are looking for您可以使用内置函数 setCompoundDrawablesWithIntrinsicBounds() 的 editText 视图(这里是 txview)来执行您要查找的操作

in my code I Used it like this .在我的代码中,我像这样使用它。 txview.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_arrow_drop_up,0); 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);

            }
        });

Hide Drawable using this使用这个隐藏 Drawable

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM