我创建了一个自定义SearchView,但到目前为止,如果用户点击了searchView小部件,我找不到(点击)事件或监听器。所以SearchView很接近,用户点击搜索图标(哪个监听器?!),SearchView被打开。见下图。我需要该事件,因为我想在用户打开searchView后更改UI
修改 我到目前为止尝试的是
setOnClickListener
到SearchView元素 - >但只有在SearchView打开时才会触发(第2张图片)
搜索了SearchView
Api,但是没有听众表示该行动(或者我是否会失明?)
尝试使用searchView的onWindowFocusChanged
解决方法,但由于searchView隐藏在某种youtube / facebook侧边栏中,这不起作用
有什么想法吗?我也附上了我的SearchView代码,但不认为代码是错误的。只是没有听众。顺便说一句:欢迎任何变通方法。但如果SearchView在开始时关闭,那就太好了! (如上图所示)
public class AmplifySearchView extends SearchView implements
OnQueryTextListener, OnFocusChangeListener,
OnCloseListener {
private static final String DEBUG_TAG = "AmplifySeachView";
private Context context;
private DataBaseController datasource;
private StationListView searchResultListView;
private TextView noResultTextView;
private Boolean searchStarted = false;
public AmplifySearchView(Context context) {
super(context);
this.context = context;
}
public AmplifySearchView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void initSearchView(StationListView view, TextView noResultTextView) {
this.searchResultListView = view;
this.searchResultListView.setActivityId(Constants.UI_SEARCH_RESULT_LIST);
this.noResultTextView = noResultTextView;
this.setOnQueryTextListener(this);
this.setOnFocusChangeListener(this);
this.setOnCloseListener(this);
this.setOnCloseListener(this);
setIconifiedByDefault(true);
datasource = DataBaseController.getInstance(context);
int id = this.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) findViewById(id);
textView.setTextColor(Color.WHITE);
resetSearchInputTextField();
}
private void resetSearchInputTextField() {
setQueryHint(getResources().getString(R.string.search_hint));
}
@Override
public boolean onClose() {
Log.d(DEBUG_TAG, "onClose()");
searchResultListView.setVisibility(View.GONE);
noResultTextView.setVisibility(View.GONE);
searchStarted = false;
Intent intent = new Intent(Constants.SEARCH_ENDED);
intent.addCategory(Constants.UI_AMPLIFY_CATEGORY);
context.sendBroadcast(intent);
return false;
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d(DEBUG_TAG,"onFocusChange()" + hasFocus);
}
@Override
public boolean onQueryTextChange(String newText) {
//do sth...
}
@Override
public boolean onQueryTextSubmit(String query) {
Log.d(DEBUG_TAG, "onQueryTextSubmit -> " + query);
return true;
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction,
Rect previouslyFocusedRect) {
Log.d(DEBUG_TAG, "onQueryTextSubmit -> " + gainFocus);
// TODO Auto-generated method stub
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
}
@Override
public void onActionViewCollapsed() {
Log.d(DEBUG_TAG, "onActionViewCollapsed");
super.onActionViewCollapsed();
}
@Override
public void onActionViewExpanded() {
Log.d(DEBUG_TAG, "onActionViewExpanded");
super.onActionViewExpanded();
}
}
btw:on焦点更改在这里不起作用,因为searchview在开头是不可见的,稍后会在ui中插入...
答案 0 :(得分:55)
这个怎么样?
public void setOnSearchClickListener (View.OnClickListener listener)
android开发者网站的描述说.. 设置一个侦听器,以便在按下搜索按钮时通知。这仅在默认情况下文本字段不可见时才相关。调用setIconified(false)也可以使这个监听器得到通知。
答案 1 :(得分:8)
public void setOnSearchClickListener (View.OnClickListener listener)
解决了这个问题。实际上我以前一直在使用这个监听器,但OnSearchClickListener
不是我的SearchView中的OnClickListener
。您必须从外部声明该侦听器,如
AmplifySearchView sv = (AmplifySearchView) findViewById(...);
sv.setOnSearchClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("CLICK","CLICK");
}
});
答案 2 :(得分:7)
用于文本字段可见的使用:
SearchView searchView = (SearchView) findViewById(R.id.searchView);
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your code here
}
});
用于文本字段隐身使用:
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
//your code here
return false;
}
});
答案 3 :(得分:3)
在Kotlin中试试这个:
searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
if (hasFocus) {
// searchView expanded
} else {
// searchView not expanded
}
}
在Java中:
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// searchView expanded
} else {
// searchView not expanded
}
}
});
答案 4 :(得分:2)
这是用API 15编写的。它尚未经过测试。详情见底。
public class MySearchView extends SearchView {
private boolean mHasFocus;
private OnOpenListener mOnOpenListener;
public MySearchView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setOnOpenListener(OnOpenListener onOpenListener) {
mOnOpenListener = onOpenListener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean onTouchEvent = super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!mHasFocus) {
mHasFocus = true;
if (mOnOpenListener != null) {
mOnOpenListener.onOpen();
}
}
break;
}
return onTouchEvent;
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
if (!gainFocus) {
mHasFocus = false;
}
}
public interface OnOpenListener {
public void onOpen();
}
}
您给它一个OnOpenListener,当单击View时,它将触发该事件。当它失去焦点时,它将重置布尔值。我认为它也将触发已经内置的OnClose。可能存在的问题是焦点位;在使用SearchView之前我遇到了问题,它不会放弃它的焦点。也许你可以在视图有焦点时分配到课外。如果你想要更多的功能,你必须建立自己的。