我的应用程序中有editbox。在editbox旁边,我有一个搜索图标。每当用户输入任何单个字符时,我想用一个或多个图标(十字图标)替换搜索图标,如果用户再次从编辑框中删除了所有他/她的文本,我想要搜索图标。
为此,我使用了以下代码。
mSearchView.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence searchString, int start, int before, int count) {
initCloseSearchView();
if (mDealerAdapter != null) {
mDealerAdapter.getFilter().filter(searchString);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
log.d(TAG, "inside afterTextChanged");
}
});
private void initCloseSearchView(){
int textLength = mSearchView.length();
log.d(TAG, "textLength "+textLength);
if(textLength >= 1){
searchIcon.setVisibility(View.GONE);
View searchView = (View) mLayoutSearch.findViewById(R.id.search);
ViewGroup parent = (ViewGroup)searchView.getParent();
int indexOfSearchView = parent.indexOfChild(searchView);
log.d(TAG, "index of search==>"+indexOfSearchView);
ImageButton closeSearch = new ImageButton(getActivity().getApplicationContext());
closeSearch.setBackgroundResource(R.drawable.close_search);
mLayoutSearch.addView(closeSearch, indexOfSearchView);
}else{
searchIcon.setVisibility(View.VISIBLE);
}
}
使用上面的代码我可以用十字图标替换搜索图标但是当用户从编辑框中删除他/她的文本时,十字图标会出现多次。如何解决这个问题。
由于
答案 0 :(得分:2)
mSearchView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence searchString, int start, int before, int count) {
initCloseSearchView();
if(count == 0){
// set first image
}else{
// set second image
}
if (mDealerAdapter != null) {
mDealerAdapter.getFilter().filter(searchString);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
log.d(TAG, "inside afterTextChanged");
}
});
根据您的要求设置图片
答案 1 :(得分:1)
这是一个样本;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xxx);
initCloseSearchView();
}
mSearchView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence searchString, int start, int before, int count) {
if (mDealerAdapter != null) {
mDealerAdapter.getFilter().filter(searchString);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable s) {
log.d(TAG, "inside afterTextChanged");
boolean isShowSearch = s.getText().toString().length() > 0 ?false:true;
updateCloseSearchView(isShowSearch);
}
});
private void initCloseSearchView(){
/* int textLength = mSearchView.length();
log.d(TAG, "textLength "+textLength);
if(textLength >= 1){
searchIcon.setVisibility(View.GONE);
View searchView = (View) mLayoutSearch.findViewById(R.id.search);
ViewGroup parent = (ViewGroup)searchView.getParent();
int indexOfSearchView = parent.indexOfChild(searchView);
log.d(TAG, "index of search==>"+indexOfSearchView);
ImageButton closeSearch = new ImageButton(getActivity().getApplicationContext());
closeSearch.setBackgroundResource(R.drawable.close_search);
mLayoutSearch.addView(closeSearch, indexOfSearchView);
}else{
searchIcon.setVisibility(View.VISIBLE);
} */
//init the view
//TODO
closeSearch.setBackgroundResource(R.drawable.close_search);
closeSearch.setVisibility(View.GONE);
searchIcon.setVisibility(View.VISIBLE);
}
private void updateIcon(boolean isShowSearch){
if(isShowSearch){
closeSearch.setVisibility(View.GONE);
searchIcon.setVisibility(View.VISIBLE);
}else{
closeSearch.setVisibility(View.VISIBLE);
searchIcon.setVisibility(View.GONE);
}
}
顺便说一下。 Member变量应以前缀m开头。 searchIcon - > mSearchIcon。 closeSearch - > mCloseSearch。
答案 2 :(得分:0)
因为在文本长度为0时你没有注意, 每次用户输入一些文本时,您都添加了一个视图。当用户删除editText中的文本时,您需要删除该视图。
创建一个标志并在添加closeSearch视图时将其设置为1,如果在else部分中它等于1,则检查该标志。如果等于1则删除closeSearch View。