2016-07-19 80 views
1

我想要覆蓋默認操作,當用戶按下十字會出現在searchview的textview的結尾處並清除搜索查詢。覆蓋searchview清除文本按鈕

我想清除搜索查詢以及打開另一個活動。

我怎麼能重寫呢?

+0

發表你已經嘗試? – Ironman

+0

沒什麼,說實話。我不知道從哪裏開始。我無法參考十字按鈕來設置其onclicklistener。 –

+1

訪問此:http://stackoverflow.com/questions/24794377/android-capture-searchview-text-clear-by-clicking-x-button – Ironman

回答

-1
@SuppressLint("NewApi") 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu items for use in the action bar 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.country_list_activity_actions, menu); 
    mSearchMenu = menu.findItem(R.id.action_search); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     // Get the SearchView and set the searchable configuration 
     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); 

     // Assumes current activity is the searchable activity 
     mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
     mSearchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default 

     // Get the search close button image view 
     ImageView closeButton = (ImageView)mSearchView.findViewById(R.id.search_close_btn); 

     // Set on click listener 
     closeButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       LoggerUtils.d(LOG, "Search close button clicked"); 
       //Find EditText view 
       EditText et = (EditText) findViewById(R.id.search_src_text); 

       //Clear the text from EditText view 
       et.setText(""); 

       //Clear query 
       mSearchView.setQuery("", false); 
       //Collapse the action view 
       mSearchView.onActionViewCollapsed(); 
       //Collapse the search widget 
       mSearchMenu.collapseActionView(); 
      } 
     }); 
    } 

    // When using the support library, the setOnActionExpandListener() method is 
    // static and accepts the MenuItem object as an argument 
    mSearchMenu.setOnActionExpandListener(new OnActionExpandListener() { 

     @Override 
     public boolean onMenuItemActionExpand(MenuItem item) { 
      //Nothing to do here 
      LoggerUtils.d(LOG, "Search widget expand "); 
      return true; // Return true to expand action view 
     } 

     @Override 
     public boolean onMenuItemActionCollapse(MenuItem item) { 
      LoggerUtils.d(LOG, "Search widget collapsed "); 
      return true; // Return true to collapse action view 
     } 
    }); 

    return super.onCreateOptionsMenu(menu); 
}