将事件侦听器与来自其他 class android 的上下文一起使用
Use event listener with context from other class android
我正在尝试使用在 searchview
折叠或展开时执行的 Searchview
Class。由于我是编程新手,所以我不太了解如何在 MainActivity
class 中使用 searchview
class。我已经试过了,但是下面的 Toast
没有显示:
MainActivity.java:
MySearchView mySearchView = new MySearchView(searchView.getContext()); // I think the problem is in this line...
mySearchView.setOnSearchViewCollapsedEventListener(new MySearchView.OnSearchViewCollapsedEventListener() {
@Override
public void onSearchViewCollapsed() {
Toast.makeText(MainActivity.this, "YES, IT'S WORKING", Toast.LENGTH_SHORT).show();
}
});
MySearchView.java:
public class MySearchView extends SearchView {
OnSearchViewCollapsedEventListener mSearchViewCollapsedEventListener;
OnSearchViewExpandedEventListener mOnSearchViewExpandedEventListener;
public MySearchView(Context context) {
super(context);
}
@Override
public void onActionViewCollapsed() {
if (mSearchViewCollapsedEventListener != null)
mSearchViewCollapsedEventListener.onSearchViewCollapsed();
super.onActionViewCollapsed();
}
@Override
public void onActionViewExpanded() {
if (mOnSearchViewExpandedEventListener != null)
mOnSearchViewExpandedEventListener.onSearchViewExpanded();
super.onActionViewExpanded();
}
public interface OnSearchViewCollapsedEventListener {
public void onSearchViewCollapsed();
}
public interface OnSearchViewExpandedEventListener {
public void onSearchViewExpanded();
}
public void setOnSearchViewCollapsedEventListener(OnSearchViewCollapsedEventListener eventListener) {
mSearchViewCollapsedEventListener = eventListener;
}
public void setOnSearchViewExpandedEventListener(OnSearchViewExpandedEventListener eventListener) {
mOnSearchViewExpandedEventListener = eventListener;
}
}
有人能帮帮我吗?
感谢您的帮助!
您可以将 MySeachView 直接添加到您的布局中
<your.package.name.MySearchView
android:id="@+id/my_search_view"
...
/>
</your.package.name.MySearchView>
并在您的 MainActivity 中使用 findViewById(R.id.my_search_view) 获取它。
在此你可以设置你的监听器。
我正在尝试使用在 searchview
折叠或展开时执行的 Searchview
Class。由于我是编程新手,所以我不太了解如何在 MainActivity
class 中使用 searchview
class。我已经试过了,但是下面的 Toast
没有显示:
MainActivity.java:
MySearchView mySearchView = new MySearchView(searchView.getContext()); // I think the problem is in this line...
mySearchView.setOnSearchViewCollapsedEventListener(new MySearchView.OnSearchViewCollapsedEventListener() {
@Override
public void onSearchViewCollapsed() {
Toast.makeText(MainActivity.this, "YES, IT'S WORKING", Toast.LENGTH_SHORT).show();
}
});
MySearchView.java:
public class MySearchView extends SearchView {
OnSearchViewCollapsedEventListener mSearchViewCollapsedEventListener;
OnSearchViewExpandedEventListener mOnSearchViewExpandedEventListener;
public MySearchView(Context context) {
super(context);
}
@Override
public void onActionViewCollapsed() {
if (mSearchViewCollapsedEventListener != null)
mSearchViewCollapsedEventListener.onSearchViewCollapsed();
super.onActionViewCollapsed();
}
@Override
public void onActionViewExpanded() {
if (mOnSearchViewExpandedEventListener != null)
mOnSearchViewExpandedEventListener.onSearchViewExpanded();
super.onActionViewExpanded();
}
public interface OnSearchViewCollapsedEventListener {
public void onSearchViewCollapsed();
}
public interface OnSearchViewExpandedEventListener {
public void onSearchViewExpanded();
}
public void setOnSearchViewCollapsedEventListener(OnSearchViewCollapsedEventListener eventListener) {
mSearchViewCollapsedEventListener = eventListener;
}
public void setOnSearchViewExpandedEventListener(OnSearchViewExpandedEventListener eventListener) {
mOnSearchViewExpandedEventListener = eventListener;
}
}
有人能帮帮我吗? 感谢您的帮助!
您可以将 MySeachView 直接添加到您的布局中
<your.package.name.MySearchView
android:id="@+id/my_search_view"
...
/>
</your.package.name.MySearchView>
并在您的 MainActivity 中使用 findViewById(R.id.my_search_view) 获取它。
在此你可以设置你的监听器。