如何解决设置工具栏图标以响应点击
经过大量研究,我发现了这段代码,我可以用它在工具栏中填充图标。 我的工具栏上有两个图标,一个是三个点状图标,另一个是搜索图标,IDE已经生成了一个图标的功能,并且工作正常,还没有弄清楚如何对搜索图标进行编程,以请求EditText的键盘焦点在同一工具栏中... 我的代码在下面
//Code to populate the toolbar with icons
public override bool OnCreateOptionsMenu(IMenu menu)
{
//This one inflates a three dotted menu to the rightmost part of the toolbar
MenuInflater.Inflate(Resource.Menu.menu2,menu);
//This one adds a search icon next to the Three dotted menu
MenuInflater.Inflate(Resource.Menu.menu4,menu);
return true;
}
//This method handles the functionality of the three dot menu icon by generating the associated menu
public override bool OnOptionsItemSelected(IMenuItem item)
{
int id = item.ItemId;
if (id == Resource.Id.action_settings)
{
Intent intent = new Intent(this,typeof(SecondActivity));
StartActivity(intent);
OverridePendingTransition(Resource.Animation.slide_in_right,Resource.Animation.abc_popup_exit);
return true;
}
return base.OnOptionsItemSelected(item);
}
//I need code here to make the search icon do something,some kind of inbuilt method or something thanks
如何使用内置方法或任何其他方式对添加到工具栏的其他图标进行编程肯定会受到感谢...
解决方法
您可以尝试创建menu.xml
并按如下所示设置搜索项:
menu.xml :
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_search"
android:icon="@drawable/fiveplus"
android:title="search"
app:showAsAction="collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
然后在您的活动中
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu,menu);
searchMenuItem = menu.FindItem(Resource.Id.menu_search);
Android.Support.V7.Widget.SearchView searchView = (Android.Support.V7.Widget.SearchView)searchMenuItem.ActionView;
searchView.QueryTextChange += SearchView_QueryTextChange;
return true;
}
private void SearchView_QueryTextChange(object sender,Android.Support.V7.Widget.SearchView.QueryTextChangeEventArgs e)
{
//do something you want
Toast.MakeText(this,e.NewText,ToastLength.Short).Show();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。