如何在 Android Edittext 上以编程方式设置 drawableRight?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22297073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically set drawableRight on Android Edittext?
提问by Sanket Kachhela
I know about set drawableRight in XML. but i required to do it programmatically because it is change as per some condition.
我知道在 XML 中设置 drawableRight。但我需要以编程方式进行,因为它会根据某些条件进行更改。
回答by Lawrence Choy
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顺序为:左、上、右、下
回答by Rethinavel
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");
回答by Sagar Maiyad
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);
回答by misbahm3
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.
然后,您可以向该特定可绘制对象添加触摸侦听器。
回答by Renjith Krishnan
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. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
将 Drawables(如果有)设置为出现在文本的左侧、上方、右侧和下方。如果您不想在那里使用 Drawable,请使用 0。Drawables 的边界将设置为它们的内在边界。
回答by Paul Mathew
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);
回答by abir99
For changing left and right both at a time I use this single line.
为了一次改变左右,我使用这条线。
download.setCompoundDrawablesWithIntrinsicBounds( R.drawable.ic_lock_open_white_24dp, 0, R.drawable.ic_lock_open_white_24dp, 0);
回答by MarGin
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);
回答by Keshav Gera
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);