版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingfeng812/article/details/51548722
android.support.v7.widget.SearchView开发记录(一)
问题列表
SearchView焦点问题
mSearchView.clearFocus();
SearchView展开状态
mSearchView.setIconified(false)
SearchView获取输入框:SearchView.SearchAutoComplete
mEdit = (SearchView.SearchAutoComplete) mSearchView.findViewById(R.id.search_src_text)
SearchView 查询图标默认位于编辑框内
mSearchView.setIconifiedByDefault(false);
SearchView修改样式
找到V7包下关于SearchView 资源布局文件,根据id来取得相关子View控件,然后设置子View的样式;关于设置自定义样式,主要是关于Shape,Laylist,Selector的设置
mEdit = (SearchView.SearchAutoComplete) mSearchView.findViewById(R.id.search_src_text);
关键代码
menu 布局文件:
<item
android:id="@+id/action_search"
android:icon="@mipmap/icon_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="@string/menu_find"
app:showAsAction="ifRoom" />
activity 关键代码:
SearchView.SearchAutoComplete mEdit;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "MainSearchActivity:onCreateOptionsMenu:140:" + "onCreateOptionsMenu()");
getMenuInflater().inflate(R.menu.menu_search, menu);
final MenuItem item = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(item);
mSearchView.setIconifiedByDefault(false);
mEdit = (SearchView.SearchAutoComplete) mSearchView.findViewById(R.id.search_src_text);
mEdit.setText(value);
mEdit.setSelection(value.length());
mSearchView.setQueryHint("输入您感兴趣的...");
final LinearLayout search_edit_frame= (LinearLayout) mSearchView.findViewById(R.id.search_edit_frame);
search_edit_frame.setBackgroundResource(R.drawable.shape_from_edit);
search_edit_frame.setClickable(true);
mEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
search_edit_frame.setPressed(hasFocus);
}
});
mEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
search_edit_frame.setPressed(true);
}
});
mEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
/*判断是否是“GO”键*/
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
/*隐藏软键盘*/
mSearchView.clearFocus();
search_edit_frame.setPressed(false);
KeyBoardUtils.closeKeybord(mEdit, ct);
//清空
if (!StringUtils.isEmpty(v.getText().toString())) {
contentlists.clear();
value = v.getText().toString();
page = 1;
resqustData(page);
}
return true;
}
return false;
}
});
return true;
}