Java 如何在文本字段中添加带有文本的搜索图标作为提示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22041733/
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 add search icon with text as hint in Text Field ?
提问by user3350437
I want to add a search icon with text as hint in text field as photo below
我想在文本字段中添加一个带有文本作为提示的搜索图标,如下图
xml file
xml文件
<EditText
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="search" />
</EditText>
回答by Joel Fernandes
Try
尝试
android:drawableLeft="@drawable/your_icon"
-
——
<EditText
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/your_icon"
android:hint="search" />
</EditText>
回答by Andynedine
Use
用
<EditText
...
android:drawableLeft="@drawable/my_icon" />
回答by Anil kumar
If You dont want click on that Image, Then you can use drawableRight property for EditText..
如果您不想单击该图像,则可以将 drawableRight 属性用于 EditText ..
android:drawableRight="@drawable/icon"
If you want click then use below code.
如果你想点击然后使用下面的代码。
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter search key" />
<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/search"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:text="Button"/>
回答by Rishabh Bhardwaj
Use this property of Editext
使用 Edittext 的这个属性
android:drawableLeft="@drawable/your_image_name"
回答by Zied R.
You can add a drawable to your EditText:
您可以向 EditText 添加一个 drawable:
<EditText
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="search"
android:drawableLeft="@drawable/icon_search" />
</EditText>
回答by Vigneshwaran.m
Try:
尝试:
<EditText
android:id="@+id/text1"
android:layout_below="@id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="search"
android:drawableLeft="@drawable/youricon" />
in the onFocusChange
of the EditText
.
在onFocusChange
的EditText
。
final EditText et=(EditText) findViewById(R.id.text1);
et.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View arg0, boolean gotfocus)
{
// TODO Auto-generated method stub
if(gotfocus)
{
et.setCompoundDrawables(null, null, null, null);
}
else if(!gotfocus)
{
if(et.getText().length()==0)
et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.youricon, 0, 0, 0);
}
}
});
回答by user3213851
You can do it dynamically. As soon as the person clicks the textfield, in the java file you can set drawableable left to none..
你可以动态地做到这一点。一旦该人单击文本字段,您就可以在 java 文件中将 drawable left 设置为 none..
回答by Padma Kumar
//that is the SearchView
//那就是 SearchView
<SearchView
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="search"
>
</SearchView>
回答by Kartihkraj Duraisamy
Have a look at this :
看看这个:
https://developer.android.com/reference/android/R.attr.html#searchHintIcon
https://developer.android.com/reference/android/R.attr.html#searchHintIcon
From API Level 22 Only
仅限 API 级别 22
回答by filippo mazzei
to work you add:
工作你添加:
android:imeOptions="actionSearch"
android:inputType="text"
<EditText
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="search"
android:imeOptions="actionSearch"
android:inputType="text"
/>