隐藏或删除旋转器Xamarin.Android
本文关键字:Xamarin Android 旋转 删除 隐藏 | 更新日期: 2023-09-27 18:01:48
我使用OnCreateOptionsMenu
让用户搜索给定的列表,然后填充一个微调器。我面临的问题是两件事之一。旋转器在用户单击搜索之前是可见的,它总是用列表中的第一个项目填充。我的问题是如何禁用/隐藏/删除旋转器,直到用户开始搜索,我如何阻止第一个项目被填充?
也有可能当用户单击搜索框,旋转器完全展开,而不是不得不点击它来显示结果?
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>
布局。主要:
<?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;
}
这是您问题的最佳解决方案。
https://stackoverflow.com/a/15162795/5920347我希望这有帮助。问候。
试试这个
<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" />
XML Attributes Spinner我没有直接看到隐藏的属性,您可以在这里再次阅读我粘贴的页面。https://developer.android.com/reference/android/widget/Spinner.html