我想在EditText提示中添加搜索图标。 实际上,我正在使用与SearchView相同的EditText,因为我想在EditText之间设置一个搜索图标,当用户将某些内容写入EditText时,该文本消失,同时提示文本消失
请查看以下屏幕截图:
答案 0 :(得分:0)
创建菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_search_album"
android:icon="@drawable/ic_search"
android:title="@string/str_search_album"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always" />
</menu>
添加提示只需将hint
属性用于菜单
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
了解更多信息bolg
Menifest.xml中的
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
有关如何 Creating a Search Interface
的详细信息在jave
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_photos_activity, menu);
final MenuItem item = menu.findItem(R.id.menu_search_photos);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
List<MyAlbumItemPojo.DataFields> filteredModelList = filter(arrDataFields, newText);
if (filteredModelList.size() > 0) {
tvDataNotFoundAlbumItem.setVisibility(View.GONE);
return true;
} else {
tvDataNotFoundAlbumItem.setVisibility(View.VISIBLE);
tvDataNotFoundAlbumItem.setText(R.string.str_photo_not_fund);
return false;
}
}
private List<MyAlbumItemPojo.DataFields> filter(List<MyAlbumItemPojo.DataFields> models, String query) {
query = query.toLowerCase();
final List<MyAlbumItemPojo.DataFields> filteredModelList = new ArrayList<>();
for (MyAlbumItemPojo.DataFields model : models) {
final String text = model.getImagename().toLowerCase().toString();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
if (!query.toString().toString().trim().equals("")){
adapter = new MyAlbumAdapter(MyAlbumsActivity.this, (ArrayList<MyAlbumItemPojo.DataFields>) filteredModelList,VisitUserID,VisitUser);
recyclerViewMyAlbums.setAdapter(adapter);
adapter.notifyDataSetChanged();
}else {
adapter = new MyAlbumAdapter(MyAlbumsActivity.this, arrDataFields,VisitUserID,VisitUser);
recyclerViewMyAlbums.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
return filteredModelList;
}
答案 1 :(得分:0)
将drawableLeft
设置为您的edittext
android:drawableLeft="@drawable/fi_search"
并隐藏当您在editText
中输入一些文字并且可见时,在其中输入任何内容时
像
csSearchTxt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() == 0) {
csSearchTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.fi_search, 0, 0, 0);
}
else
{
csSearchTxt.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 2 :(得分:0)
请使用Spannable字符串和ImageSpan在edittext
中添加图标作为提示 ImageSpan imageHint = new ImageSpan(this, R.drawable.ic_search_accent_24dp);
String blankString = " ";
SpannableString spannableString = new SpannableString(blankString);
spannableString.setSpan(imageHint, 0, blankString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
searchEdit.setHint(spannableString);
答案 3 :(得分:0)
在xml中添加到您的EditText:android:drwableLeft="@drawable/imageSearch"
这会在EditText中放置一个图像,但在您输入文本后它仍然存在。 要使图像消失,只需覆盖您的活动:
@Override
public void afterTextChanged(Editable editable) {
if(editable.toString().length() > 0) {
searchEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
} else {
searchEditText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image, 0, 0, 0);
}
}
应该做的伎俩