Mic*_*Bak 99 android android-3.0-honeycomb android-4.0-ice-cream-sandwich android-actionbar
我正在尝试添加SearchView对Android 3.0+ ActionBar的支持,但我无法让它OnCloseListener工作.
这是我的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
searchView = (SearchView) menu.findItem(R.id.search_textbox).getActionView();
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
searchLibrary(newText);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) { return false; }
});
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
System.out.println("Testing. 1, 2, 3...");
return false;
}
});
return true;
}
Run Code Online (Sandbox Code Playgroud)
搜索效果很好,每个人都在工作,除了OnCloseListener.没有任何东西被打印到Logcat.这是我按下"关闭"按钮时的Logcat:
02-17 13:01:52.914: I/TextType(446): TextType = 0x0
02-17 13:01:57.344: I/TextType(446): TextType = 0x0
02-17 13:02:02.944: I/TextType(446): TextType = 0x0
Run Code Online (Sandbox Code Playgroud)
我查看了文档和示例,但似乎没有任何改变.我在冰淇淋三明治上的Asus Transformer Prime和Galaxy Nexus上运行它.有任何想法吗?
更新:
是的 - System.out.println() 确实有效.这是证据:
@Override
public boolean onQueryTextChange(String newText) {
System.out.println(newText + "hello");
searchLibrary(newText);
return false;
}
Run Code Online (Sandbox Code Playgroud)
结果在这个Logcat中:
02-17 13:04:20.094: I/System.out(21152): hello
02-17 13:04:24.914: I/System.out(21152): thello
02-17 13:04:25.394: I/System.out(21152): tehello
02-17 13:04:25.784: I/System.out(21152): teshello
02-17 13:04:26.064: I/System.out(21152): testhello
Run Code Online (Sandbox Code Playgroud)
小智 143
我也遇到了这个问题,我别无选择,只能放弃"oncloselistener".相反,你可以得到你的menuItem setOnActionExpandListener.然后覆盖unimplents方法.
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
Log.d("*******","onMenuItemActionExpand");
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//do what you want to when close the sesarchview
//remember to return true;
Log.d("*******","onMenuItemActionCollapse");
return true;
}
Run Code Online (Sandbox Code Playgroud)
lom*_*mza 61
对于Android API 14+(ICS和更高版本),请使用以下代码:
// When using the support library, the setOnActionExpandListener() method is
// static and accepts the MenuItem object as an argument
MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请访问:http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
参考:onActionCollapse/onActionExpand
小智 29
对于这个问题我想出了这样的事情,
private SearchView mSearchView;
@TargetApi(14)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.conversation_index_activity_menu, menu);
mSearchView = (SearchView) menu.findItem(R.id.itemSearch).getActionView();
MenuItem menuItem = menu.findItem(R.id.itemSearch);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
menuItem.setOnActionExpandListener(new OnActionExpandListener()
{
@Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
// Do something when collapsed
Log.i(TAG, "onMenuItemActionCollapse " + item.getItemId());
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item)
{
// TODO Auto-generated method stub
Log.i(TAG, "onMenuItemActionExpand " + item.getItemId());
return true;
}
});
} else
{
// do something for phones running an SDK before froyo
mSearchView.setOnCloseListener(new OnCloseListener()
{
@Override
public boolean onClose()
{
Log.i(TAG, "mSearchView on close ");
// TODO Auto-generated method stub
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*rio 18
我在android 4.1.1上遇到了同样的问题.看起来这是一个已知的错误:https://code.google.com/p/android/issues/detail?id = 25758
无论如何,作为一种解决方法,我使用状态更改侦听器(当SearchView与操作栏分离时,它也明显关闭).
view.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
@Override
public void onViewDetachedFromWindow(View arg0) {
// search was detached/closed
}
@Override
public void onViewAttachedToWindow(View arg0) {
// search was opened
}
});
Run Code Online (Sandbox Code Playgroud)
以上代码在我的案例中运作良好.
我在这里发布相同的答案:https://stackoverflow.com/a/24573266/2162924
Mic*_*Bak 10
我最终使用了一些黑客,这对我的目的很有效 - 不确定它是否适用于所有目的.无论如何,我正在检查搜索查询是否为空.这是不是真的涉及到SearchView的OnCloseListener,虽然-这仍然无法正常工作!
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
// Search
} else {
// Do something when there's no input
}
return false;
}
@Override
public boolean onQueryTextSubmit(String query) { return false; }
});
Run Code Online (Sandbox Code Playgroud)
好吧,这解决了我的问题:
菜单项 showAsAction="always"
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
Run Code Online (Sandbox Code Playgroud)
和活动
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
Log.i("SearchView:", "onClose");
searchView.onActionViewCollapsed();
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
为了使OnCloseListener工作正常进行,请确保在搜索菜单项中将其showAsAction设置为。always
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".SearchActivity">
<item
android:id="@+id/search"
android:title="@string/search"
android:icon="@drawable/ic_search_toolbar"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49874 次 |
| 最近记录: |