Android手冊(cè)之Toolbar搜索聯(lián)動(dòng)及監(jiān)聽(tīng)小技巧
??實(shí)踐過(guò)程
??監(jiān)聽(tīng)
//返回事件的監(jiān)聽(tīng),也就是app:navigationIcon屬性設(shè)置的圖片的監(jiān)聽(tīng)
idToolBar.setNavigationOnClickListener {
Toast.makeText(this, "點(diǎn)擊了返回", Toast.LENGTH_SHORT).show()
}
//右側(cè)點(diǎn)擊的監(jiān)聽(tīng)
idToolBar.setOnMenuItemClickListener {
Toast.makeText(this, it.title, Toast.LENGTH_SHORT).show()
true
}
??實(shí)現(xiàn)搜索
<?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/idTooBarMenuOne"
android:icon="@mipmap/icon_xin_no"
android:orderInCategory="1"
android:title="搜索"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView " />
</menu>
重點(diǎn)為app:showAsAction和app:actionViewClass屬性,actionViewClass表示展開(kāi)哪個(gè)view,上面的android.widget.SearchView是系統(tǒng)的搜索view。
<androidx.appcompat.widget.Toolbar
android:id="@+id/idToolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000ff"
app:collapseIcon="@mipmap/icon_xin_yes"
app:menu="@menu/menu"
app:titleTextColor="#ff0000" />

關(guān)于搜索的幾個(gè)監(jiān)聽(tīng)事件
//注意import引入的SearchView 要和 menu的item中app:actionViewClass屬性一致
val searchView: SearchView = idToolBar.menu.findItem(R.id.idTooBarMenuOne).actionView as SearchView
//設(shè)置搜索提示語(yǔ)
searchView.queryHint = "搜索"
searchView.setOnSearchClickListener {
//當(dāng)點(diǎn)擊搜索編輯框的時(shí)候回調(diào),剛進(jìn)入的時(shí)候默認(rèn)是點(diǎn)擊搜索編輯框的
Toast.makeText(this@ActivityToolBar, "點(diǎn)擊了搜索框", Toast.LENGTH_SHORT).show()
}
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(s: String?): Boolean {
//點(diǎn)擊手機(jī)軟鍵盤搜索按鈕的時(shí)候回調(diào)
Toast.makeText(this@ActivityToolBar, "點(diǎn)擊了搜索", Toast.LENGTH_SHORT).show()
return false
}
override fun onQueryTextChange(s: String?): Boolean {
//當(dāng)搜索框中文本發(fā)生改變的時(shí)候執(zhí)行
return false
}
})
searchView.setOnQueryTextFocusChangeListener { v, hasFocus ->
//當(dāng)?shù)玫浇裹c(diǎn)和失去焦點(diǎn)的時(shí)候執(zhí)行
if (hasFocus) {
Toast.makeText(this@ActivityToolBar, "獲取了焦點(diǎn)", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this@ActivityToolBar, "失去了焦點(diǎn)", Toast.LENGTH_SHORT).show()
}
}
idToolBar.menu.findItem(R.id.idTooBarMenuOne).setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
//展開(kāi)時(shí)執(zhí)行
Toast.makeText(this@ActivityToolBar, "進(jìn)行了展開(kāi)", Toast.LENGTH_SHORT).show()
return true
}
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
//收起時(shí)回調(diào)
Toast.makeText(this@ActivityToolBar, "進(jìn)行了收縮", Toast.LENGTH_SHORT).show()
return true
}
})
??改變Overflow
OverFlow就是溢出的那三個(gè)點(diǎn)點(diǎn)。可以自己設(shè)置其他的圖標(biāo),比如一個(gè)加號(hào)。而且推薦這種方式,其他在theme文件中設(shè)置style的屬性不太好,官方對(duì)這塊支持還差點(diǎn)。
idToolBar.overflowIcon=ContextCompat.getDrawable(this,R.mipmap.dragon_eight)
??修改Menu彈出位置
只需要在你的主題中設(shè)置overlapAnchor屬性節(jié)課,默認(rèn)是true會(huì)從右上角展示,false為在toolbar下方展示。

當(dāng)然嗎,處了上面屬性,還可以在其他文章中看到關(guān)于在這修改Menu的樣式啥的,但其實(shí)不建議在這搞,因?yàn)檫@是主題,不僅僅會(huì)應(yīng)用到這,還會(huì)應(yīng)用到你的其他地方,都是統(tǒng)一的還好,可如果不是,你的處理就麻煩了。
以上就是Android手冊(cè)之Toolbar搜索聯(lián)動(dòng)及監(jiān)聽(tīng)小技巧的詳細(xì)內(nèi)容,更多關(guān)于Android Toolbar搜索聯(lián)動(dòng)監(jiān)聽(tīng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Android MarginLeft與MarginStart的區(qū)別(詳解)
下面小編就為大家分享一篇基于Android MarginLeft與MarginStart的區(qū)別(詳解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Android 配置gradle實(shí)現(xiàn)VersionCode自增實(shí)例
今天小編就為大家分享一篇Android 配置gradle實(shí)現(xiàn)VersionCode自增實(shí)例,具有很好的 參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android實(shí)現(xiàn)Path平滑的涂鴉效果實(shí)例
這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)Path平滑涂鴉效果的相關(guān)資料,通過(guò)本文介紹的方法修改后會(huì)讓線條平滑很多,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Android通過(guò)json向MySQL中讀寫(xiě)數(shù)據(jù)的方法詳解【讀取篇】
這篇文章主要介紹了Android通過(guò)json向MySQL中讀寫(xiě)數(shù)據(jù)的方法,涉及Android解析json以及與php交互讀取mysql的方法,需要的朋友可以參考下2016-06-06
Android實(shí)現(xiàn)GridView中ImageView動(dòng)態(tài)變換的方法
這篇文章主要介紹了Android實(shí)現(xiàn)GridView中ImageView動(dòng)態(tài)變換的方法,以實(shí)例形式較為詳細(xì)的分析了GridView中ImageView動(dòng)態(tài)變換的頁(yè)面布局及功能實(shí)現(xiàn)相關(guān)技巧,需要的朋友可以參考下2015-10-10
Android?ViewStub使用方法學(xué)習(xí)
這篇文章主要為大家介紹了Android?ViewStub使用方法學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android實(shí)現(xiàn)下拉刷新的視圖和圖標(biāo)的旋轉(zhuǎn)
本篇文章主要介紹了Android實(shí)現(xiàn)下拉刷新的視圖和圖標(biāo)的旋轉(zhuǎn)的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03

