gpt4 book ai didi

android - 应用恢复后触发 onQueryTextChange

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:33 26 4
gpt4 key购买 nike

我对 QueryTextListener 上的动画搜索 View 感到困惑。当首先创建 Activity 和 fragment 时,它工作得很好。然后我按下主页按钮,打开其他应用程序,在那里做一些工作以删除 searchview Activity 的数据,然后返回到应用程序。当 Activity 和 fragment 恢复时,onQueryTextChange 方法由它自己触发。我试过这个问题 Fragment replacement triggers onQueryTextChange on searchview但它没有帮助,只有当 searchview SHOW_AS_ACTION_NEVER 时才有帮助,但在这种情况下我看不到 searchview。如何防止OnQueryTextListener自触发?来自 fragment 的 fragment

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

searchView = new SearchView(getSherlockActivity().getSupportActionBar()
.getThemedContext());
searchView.setOnQueryTextListener(new OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {

fpAdapter.getFilter().filter(newText);
} else {

loadData();

}
return false;
}
});

TextView searchText = (TextView) searchView
.findViewById(R.id.abs__search_src_text);
searchText.setTextColor(Color.WHITE);

searchText.setCursorVisible(false);

ImageView searchButton = (ImageView) searchView
.findViewById(R.id.abs__search_button);
searchButton.setImageResource(R.drawable.search_menu_button);
LinearLayout searchEditFrame = (LinearLayout) searchView
.findViewById(R.id.abs__search_edit_frame);
searchEditFrame.setBackgroundResource(android.R.color.transparent);

View searchPlate = searchView.findViewById(R.id.abs__search_plate);
searchPlate.setBackgroundColor(Color.argb(0, 0, 0, 0));
menu.add("Search")

.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

final MenuItem searchMenuItem = menu.getItem(0);
final Animation in = AnimationUtils.loadAnimation(getSherlockActivity()
.getApplicationContext(), R.anim.search_in);
final Animation out = AnimationUtils.loadAnimation(
getSherlockActivity().getApplicationContext(),
R.anim.search_out);
searchView.setQueryHint(getResources().getText(
R.string.search_messages_hint));
searchView.setIconifiedByDefault(true);
searchView
.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view,
boolean queryTextFocused) {

if (!queryTextFocused) {

// searchView.startAnimation(out);
searchMenuItem.collapseActionView();
} else {
searchView.startAnimation(in);

}
}
});

super.onCreateOptionsMenu(menu, inflater);
}

更新:这只出现在 HTC sensation XL with Android 4.0.3 上,在 4.2 上我没有看到这个问题。

最佳答案

找到了唯一的解决方案——在onResume中设置监听:

@Override
public void onResume() {

super.onResume();

searchView.setOnQueryTextListener(new OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {

fpAdapter.getFilter().filter(newText);
} else {

loadData();

}
return false;
}
}); }

关于android - 应用恢复后触发 onQueryTextChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25272507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com