我有一个带有drawable的EditText。我想使drawable可点击,以便在用户点击drawable时我可以执行特定操作。我怎么做?我的EditText是:
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:drawableRight="@drawable/question_mark"
android:hint="phone number"
android:imeActionId="@+id/phone_num"
android:maxLines="1"
android:singleLine="true"
android:textColor="#000000" />
答案 0 :(得分:3)
正如Gina上面提到的,你可以通过使用RelativeLayout而不是drawableRight属性来实现这一点。下面的代码将imageview放在EditText的右侧部分
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:hint="phone number"
android:imeActionId="@+id/phone_num"
android:maxLines="1"
android:singleLine="true"
android:textColor="#000000" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignRight="@+id/phone"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
答案 1 :(得分:0)
phone.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
final int DRAWABLE_RIGHT = 2;
if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
if(motionEvent.getRawX() >= (phone.getRight() - phone.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
//Here is your code when you click drawable right
return true;
}
}
return false;
}
});