恢复android的searchview小部件的状态

时间:2014-03-22 19:08:14

标签: java android android-fragments android-widget searchview

我在操作栏中有一个搜索视图,我希望在方向更改后恢复查询。我怎样才能做到这一点?使用SearchView.setQuery(query, false);从捆绑包中设置查询并不起作用,因为searchview会在展开后立即将其查询设置为""

我的onActivityCreated

中的相关代码
if(savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_KEY_SEARCH))
    mCurFilter = savedInstanceState.getString(BUNDLE_KEY_SEARCH);

这是我的onSaveInstanceState

savedInstanceState.putString(BUNDLE_KEY_SEARCH, mCurFilter);
super.onSaveInstanceState(savedInstanceState);

那么如何使用setQuery来恢复其状态呢? 感谢所有

4 个答案:

答案 0 :(得分:3)

作为解决方法,请替换

mSearchView.setQuery(mSearchText, false); 

使用:

final String s = mSearchText;

mSearchView.post(new Runnable() {

    @Override
    public void run() {
        mSearchView.setQuery(s, false);
    }
});

在系统设置空字符串后,它将设置保存的字符串。

答案 1 :(得分:1)

是的,可以恢复搜索视图小部件的状态:

使用以下代码保存搜索查询文本:

private String mSearchQuery;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();

    menuInflater.inflate(R.menu.search_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();

  searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    if(mSearchQuery != null){
        searchView.setIconified(true);
        searchView.onActionViewExpanded();
        searchView.setQuery(mSearchQuery, false);
        searchView.setFocusable(true);
    }

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){

        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            mSearchQuery = newText;
            return false;
        }
    });

    return true;
}

 @Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("searchQuery", mSearchQuery);
    super.onSaveInstanceState(outState);
}

然后在活动的OnCreate()方法中添加以下行:

    if(savedInstanceState != null){
        mSearchQuery = savedInstanceState.getString("searchQuery");
    }

答案 2 :(得分:0)

有更好的(在我看来)方式(不发布 Runnable )以避免在展开你的"" 时设置查询搜索查看即可。

1)您还需要一个 String 变量来存储查询。

//String that you fill in onQueryTextChange
String searchText;
//String, that is filled after you restore previous
String queryToSave;

2)从Bundle获取您的查询后,将其存储在另一个变量中。

if(this.searchText!=null)
{
    this.queryToSave=this.searchText;
}

3)在 onQueryTextChange(字符串查询)中,您将 searchText 变量填充到文本中,如果它是空的则为null

4)在 onMenuItemActionExpand(MenuItem item)中,您需要将已恢复的字符串( searchText )保存到另一个变量( queryToSave ),因为它( searchText )在使用空文本调用 onQueryTextChange 后将设置为null

if(searchText!=null)
{
    queryToSave=new StringBuffer(searchText).toString();
}

5)所以现在如果你打电话

mSearchView.setQuery(queryToSave, false);
展开 SearchView queryToSave 后,

不会为空,您会看到展开的 SearchView 与您的已恢复文字!< / p>

答案 3 :(得分:0)

你应该这样做:

import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    private SearchView mSearchView;
    private String mSearchString;
    private static final String SEARCH_KEY = "search";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // if you saved something on outState you can recover them here
        if (savedInstanceState != null) {
            mSearchString = savedInstanceState.getString(SEARCH_KEY);
        }
    }

    // This is called before the activity is destroyed
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mSearchString = mSearchView.getQuery().toString();
        outState.putString(SEARCH_KEY, mSearchString);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        MenuItem searchMenuItem = menu.findItem(R.id.menu_main_action_search);

        mSearchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);

        //focus the SearchView
        if (mSearchString != null && !mSearchString.isEmpty()) {
            searchMenuItem.expandActionView();
            mSearchView.setQuery(mSearchString, true);
            mSearchView.clearFocus();
        }

        return super.onCreateOptionsMenu(menu);
    }
}