android.support.v7.widget.SearchView开发记录(一)

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 布局文件:

 

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;
}

原文点击

  • android.support.v7.widget.SearchView开发记录(一)

你可能感兴趣的:(android.support.v7.widget.SearchView开发记录(一))