[英]How to add two drawableRight in EditText?
I am able to add one drawableRight
in EditText
in android easly and on click event working perfectly in case of one drawableRight
. 我能够在android中轻松地在
EditText
中添加一个drawableRight
,并且在click事件中,在一个drawableRight
情况下可以正常工作。 But I need two drawableRight
in EditText
. 但是我在
EditText
需要两个drawableRight
。
So, How can I add two drawableRight
in EditText
? 因此,如何在
EditText
添加两个drawableRight
? and I also need to perform click event on both drawableRight
separately. 而且我还需要分别对两个
drawableRight
执行click事件。
For example I want to add yellow star in EditText
like as given below image and on click on rightmost image I want to open contact book of phone and on click of yellow star I want to call user's favourite numbers list. 例如,我想在
EditText
添加黄色的星星,如下图所示,然后单击最右边的图像,我要打开电话的通讯录,然后单击黄色的星星,我要呼叫用户的收藏夹号码列表。
So How can I do this? 那么我该怎么做呢? Any Idea?
任何想法?
There is no native support for this, so you got two options here: 没有对此的本地支持,因此您在此处有两个选择:
Easy solution: Create a Linear layout with tow image views on the right side, those will be your drawables. 简单的解决方案:创建一个线性布局,并在右侧带有两个图像视图,这些将成为您的可绘制对象。
The hard way: Extend a Drawable
class and implement your own onDraw
method where you will draw the two drawables. 困难的方法:扩展
Drawable
类并实现自己的onDraw
方法,在该方法中您将绘制两个drawable。 Than use that one for your text view. 比将那个用于您的文本视图。
You can't. 你不能 The
TextView
can contain only one drawable on either of its sides. TextView
任一侧只能包含一个可绘制对象。 The only options you have are: 您唯一的选择是:
View
. View
。 ViewGroup
descendants ( RelativeLayout
/ FrameLayout
/etc) and put the TextView along with the two ImageView
s in it. ViewGroup
子孙( RelativeLayout
/ FrameLayout
/ etc),然后将TextView和两个ImageView
一起放入其中。 Just place two Drawables into EditText using RelativeLayout. 只需使用RelativeLayout将两个Drawable放入EditText中即可。 To set inner padding, place an invisible drawableRight into EditText:
要设置内部填充,请将一个不可见的drawableRight放入EditText中:
/res/values/dimens.xml /res/values/dimens.xml
<resources>
<dimen name="iconSize">32dp</dimen>
</resources>
/res/layout/my_layout.xml /res/layout/my_layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="textAutoComplete"/>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="@dimen/iconSize"
android:layout_height="@dimen/iconSize"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_action_1"/>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="@dimen/iconSize"
android:layout_height="@dimen/iconSize"
android:layout_toLeftOf="@+id/imageButton1"
android:layout_centerVertical="true"
android:src="@drawable/ic_action_2"/>
</RelativeLayout>
In your Activity: 在您的活动中:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
EditText editText = (EditText) findViewById(R.id.editText);
int iconSize = (int) getResources().getDimension(R.dimen.iconSize)
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_action_1);
drawable.setBounds(0, 0, iconSize * 2, 0); // that is the trick!
editText.setCompoundDrawables(null, null, drawable, null);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.