程序工具栏图标以响应点击
Program Toobar Icon to respond to Tap
经过大量研究后,我发现了可用于用图标填充工具栏的代码。
我的工具栏上有两个图标,一个三点图标和一个搜索图标,其中一个的功能由 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>
然后在你的 activity:
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();
}
经过大量研究后,我发现了可用于用图标填充工具栏的代码。 我的工具栏上有两个图标,一个三点图标和一个搜索图标,其中一个的功能由 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>
然后在你的 activity:
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();
}