lon*_*ngi 32 android android-layout android-searchmanager
我正在创建一个自定义SearchView,但到目前为止,如果用户点击searchView小部件,我没有找到(点击)事件或监听器.所以SearchView很接近,用户点击搜索图标(哪个监听器?!),SearchView被打开.见下图.我需要那个事件,因为我想在用户打开searchView后更改UI

编辑: 到目前为止我尝试的是
setOnClickListener 到SearchView元素 - >但它只在SearchView打开时触发(第2张图片)
搜索了SearchViewApi,但是没有听众那个动作(或者我是盲人?)
尝试使用onWindowFocusChangedsearchView 解决方法,但由于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();
}
}
Run Code Online (Sandbox Code Playgroud)
btw:on焦点更改在这里不起作用,因为searchview在开头是不可见的,后面插入ui ...
chu*_*n20 56
这个怎么样?
public void setOnSearchClickListener (View.OnClickListener listener)
Run Code Online (Sandbox Code Playgroud)
来自android开发者网站的描述说..设置一个监听器,在按下搜索按钮时通知.这仅在默认情况下文本字段不可见时才相关.调用setIconified(false)也可以使该侦听器得到通知.
public void setOnSearchClickListener (View.OnClickListener listener)
Run Code Online (Sandbox Code Playgroud)
解决了这个问题.实际上我以为我以前一直在使用这个监听器,但这OnSearchClickListener不是OnClickListener我的SearchView.你必须从外面声明听众
AmplifySearchView sv = (AmplifySearchView) findViewById(...);
sv.setOnSearchClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("CLICK","CLICK");
}
});
Run Code Online (Sandbox Code Playgroud)
对于文本字段可见使用:
SearchView searchView = (SearchView) findViewById(R.id.searchView);
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your code here
}
});
Run Code Online (Sandbox Code Playgroud)
用于文本字段隐形使用:
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
//your code here
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
在 Kotlin 中试试这个:
searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
if (hasFocus) {
// searchView expanded
} else {
// searchView not expanded
}
}
Run Code Online (Sandbox Code Playgroud)
在 Java 中:
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// searchView expanded
} else {
// searchView not expanded
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40536 次 |
| 最近记录: |