android.support.v7.widget.SearchView開發記錄(一)

雲驛站發表於2016-05-31

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 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;
}

原文點選


相關文章