使EditText成为可点击的一部分?

问题描述:

我有一个带9补丁背景的EditText:
enter image description here
是否有可能使搜索图标可点击?
谢谢!使EditText成为可点击的一部分?

+0

不,不是那样。编辑文本如何知道9补丁背景的哪一部分是搜索图标? –

如果您覆盖boolean onTouch(View v, MotionEvent event),我相信这是可能的。不过,实际上,如果您将ImageView的搜索图标放在EditText的右侧,并且只需点击ImageView即可实现。

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <EditText 
     android:layout_toLeftOf="@id/search" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <ImageView 
     android:id="@+id/search" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:src="@android:drawable/ic_search_category_default" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 
+0

onTouch的问题是要知道要计数的区域,而不是将其与文本重叠。这正确的答案是这样的。 –

+0

你总是可以添加正确的填充以确保文本不会覆盖图像...但它仍然不是我推荐的模式:) – Jon

+0

谢谢@GabeSechan是不是只能使图标的可点击部分成为可能(这是9个补丁映像的非可缩放部分)? – hereForLearing