捕获Searchview清除按钮事件

时间:2018-02-28 15:22:21

标签: android xamarin xamarin.android

我试图在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" />

1 个答案:

答案 0 :(得分: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();
};