[英]Hide or remove a spinner Xamarin.Android
I use OnCreateOptionsMenu
for users to search a given list which then populates a spinner. 我使用
OnCreateOptionsMenu
来搜索给定列表,然后填充一个微调器。 The problem I am facing is one of two things. 我面临的问题是两件事之一。 The spinner is visible before a user clicks on search and it is always populated with the first item in my list.
在用户单击搜索之前,微调器是可见的,并且始终使用列表中的第一个项目填充它。 My question is how can I disable/hide/remove the spinner untill a user starts searching and how can I stop the first item being populated?
我的问题是如何禁用/隐藏/删除微调器,直到用户开始搜索,如何停止填充第一个项目?
Also is it possible when a user clicks the search box that the spinner fully expands instead of having to click on it to show the results? 当用户点击微调器完全展开的搜索框而不必点击它来显示结果时,它是否也可以?
Menu.main: Menu.main:
<?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/action_search"
android:title="Search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Layout.Main: Layout.Main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<Spinner
android:layout_width="match_parent"
android:layout_height="49.0dp"
android:id="@+id/spinner1" />
//etc...
</LinearLayout>
MainActivity: 主要活动:
private Android.Support.V7.Widget.SearchView _searchView;
private ArrayAdapter _adapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
//list omitted
spinner = FindViewById<Spinner>(Resource.Id.spinner1);
_adapter = new ArrayAdapter<MyClass>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, list);
spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);
spinner.Adapter = _adapter;
}
private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
string toast = string.Format("Selected text is {0}", spinner.GetItemAtPosition(e.Position));
Toast.MakeText(this, toast, ToastLength.Long).Show();
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.main, menu);
var item = menu.FindItem(Resource.Id.action_search);
var searchView = MenuItemCompat.GetActionView(item);
_searchView = searchView.JavaCast<Android.Support.V7.Widget.SearchView>();
_searchView.QueryTextChange += (s, e) => _adapter.Filter.InvokeFilter(e.NewText);
_searchView.QueryTextSubmit += (s, e) =>
{
Toast.MakeText(this, "Searched for" + e.Query, ToastLength.Short).Show();
e.Handled = true;
};
return true;
}
This is the best solution for your problem. 这是解决您问题的最佳方案。
https://stackoverflow.com/a/15162795/5920347 https://stackoverflow.com/a/15162795/5920347
Try this 试试这个
<LinearLayout
android:id="@+id/contacts_type"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
//Change the visibility of the Layout. When you click the button change to visible
android:visibility="gone">
<Spinner
android:layout_width="match_parent"
android:layout_height="49.0dp"
android:id="@+id/spinner1" />
the XML Attributes Spinner I don't watch a property that hide directly you can read again here I paste the page. XML属性Spinner我没有看到直接隐藏的属性,你可以在这里再次阅读我粘贴页面。 https://developer.android.com/reference/android/widget/Spinner.html
https://developer.android.com/reference/android/widget/Spinner.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.