1.设置searchView会在Toolbar中显示,或者在普通布局中显示:
(1).在普通布局当中:
/**
* 设置searchView:
*/
setSearchAttribute(searchView);
setSearchColor(searchView);
//设置搜索栏的默认提示
searchView.setQueryHint("Leslie");
//默认刚进去就打开搜索栏
searchView.setIconified(false);
//点击事件:
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), SearchActivity.class));
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getContext(), "您输入的文本为" + query, Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
(2)在meau中设置:
/**
* 设置menu:
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//找到searchView:
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem item=menu.findItem(R.id.searchs);
SearchView searchView= (SearchView) MenuItemCompat.getActionView(item);
setSearchAttribute(searchView);
setSearchColor(searchView);
//点击事件:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(SearchActivity.this, "您输入的文本为" + query, Toast.LENGTH_SHORT).show();
searchs=query;
ShareMusicUtils.SONG_NAME=SplicingSearchs();
//请求搜索对象有关内容:
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://169.254.206.92:3000/search/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
SearchApi searchApi = retrofit.create(SearchApi.class);
Observable myStar = searchApi.getSearchData(ShareMusicUtils.SONG_NAME)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Toast.makeText(SearchActivity.this, "asdasdasdsadasdasdsada", Toast.LENGTH_SHORT).show();
myStar.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(SearchBean searchBean) {
Toast.makeText(SearchActivity.this, searchBean.toString(), Toast.LENGTH_SHORT).show();
Log.i("222222222222222", searchBean.toString());
List songs = searchBean.getResult().getSongs();
articleLists.addAll(songs);
List mvs = searchBean.getResult().getMvs();
classLists.addAll(mvs);
List playlists = searchBean.getResult().getPlaylists();
musicLists.addAll(playlists);
Log.i("1111111111111", songs.toString());
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
// Intent intent=new Intent(SearchActivity.this, SearchResultActivity.class);
// intent.putExtra("broad", (Serializable) articleLists);
// intent.putExtra("classes", (Serializable) classLists);
// intent.putExtra("article", (Serializable) musicLists);
// startActivity(intent);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
2.下面是searchView的设置方法:
(1)设置SearchView属性:
/**
* 设置Searchview属性:
*/
private SearchView setSearchAttribute(SearchView searchView) {
//默认刚进去就打开搜索栏:
searchView.setIconified(false);
//敲掉消除按键,下划线:
searchView.findViewById(android.support.v7.appcompat.R.id.search_plate).setBackground(null);
searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn).setBackground(null);
//设置字体颜色、大小、背景:
android.support.v7.widget.SearchView.SearchAutoComplete textView = searchView.findViewById(R.id.search_src_text);
textView.setTextColor(getResources().getColor(R.color.text_color_hint));
textView.setHintTextColor(getResources().getColor(R.color.text_color_hint));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
textView.setBackgroundResource(R.drawable.search_box);
//自定义button背景:
ImageView closeViewIcon = (ImageView) searchView.findViewById(R.id.search_close_btn);
closeViewIcon.setImageDrawable(ContextCompat
.getDrawable(getContext(), R.drawable.pinlun_send));
//设置搜索图标在编辑框外,ture时在框内
searchView.setIconifiedByDefault(true);
return searchView;
}
(2).设置光标:
/**
* 设置searchView光标:
*/
private SearchView setSearchColor(SearchView searchView) {
final EditText editText = (EditText) searchView.findViewById(R.id.search_src_text);
try {
Field fCursorDrawableRes = TextView.class
.getDeclaredField("mCursorDrawableRes");
fCursorDrawableRes.setAccessible(true);
int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);
Field fEditor = TextView.class.getDeclaredField("mEditor");
fEditor.setAccessible(true);
Object editor = fEditor.get(editText);
Class> clazz = editor.getClass();
Field fCursorDrawable =
clazz.getDeclaredField("mCursorDrawable");
fCursorDrawable.setAccessible(true);
if (mCursorDrawableRes <= 0) {
return searchView;
}
Drawable cursorDrawable =
ContextCompat.getDrawable(searchView.getContext(), mCursorDrawableRes);
if (cursorDrawable == null) {
return searchView;
}
Drawable tintDrawable = DrawableCompat.wrap(cursorDrawable);
DrawableCompat.setTintList(tintDrawable,
//自定义的颜色
ColorStateList.valueOf(
ContextCompat.getColor(getContext(), R.color.text_color_normal)));
Drawable[] drawables
= new Drawable[]{tintDrawable, tintDrawable};
fCursorDrawable.set(editor, drawables);
} catch (Throwable ignored) {
}
return searchView;
}