隐藏或删除微调器Xamarin.Android

时间:2016-08-10 14:14:16

标签: c# android linq android-layout xamarin

我使用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>

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;
    }

1 个答案:

答案 0 :(得分:2)

这是解决您问题的最佳方案。

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属性Spinner我没有看到直接隐藏的属性,你可以在这里再次阅读我粘贴页面。   https://developer.android.com/reference/android/widget/Spinner.html