捕获Searchview清除按钮事件

问题描述 投票:2回答:1

我正在尝试在搜索视图中捕获一个android项目的清除按钮事件:

SearchView _search = FindViewById<SearchView>(Resource.Id.publicSearchview);
_search.QueryTextSubmit += SearchBtn_Click;
ImageView _closeBtn = FindViewById<ImageView>(Resource.Id.search_close_btn);
_closeBtn.Click += CloseBtn_Click;

但_closeBtn始终为null。有什么建议??

    <SearchView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/publicSearchview" />
android xamarin xamarin.android
1个回答
1
投票

假设SearchView没有设置为icon'fy,这将起作用:

var _search = FindViewById<SearchView>(Resource.Id.searchView1);
int searchCloseButtonId = _search.Context.Resources.GetIdentifier("android:id/search_close_btn", null, null);
var _closeBtn = FindViewById<ImageView>(searchCloseButtonId);
_closeBtn.Click += (object sender, EventArgs e) => 
{ 
    Toast.MakeText(this, "Close Click", ToastLength.Long).Show();
};
© www.soinside.com 2019 - 2024. All rights reserved.