用户单击SearchView并打开键盘时如何隐藏FAB?

时间:2019-08-29 14:35:25

标签: java android xml

当我单击SearchView时如何隐藏FAB 因此FAB会在键盘打开时显示

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="18dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="8dp"
        android:scaleType="center"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_add_operator"
        app:backgroundTint="@android:color/transparent"
        app:layout_constraintEnd_toEndOf="parent"
         />

1 个答案:

答案 0 :(得分:1)

我一直在寻找相同的解决方案,并且想出了这个解决方案,它对我的​​SearchView来说很好用。检出(Kotlin版):

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        val search: SearchView =
            menu!!.findItem(R.id.action_search_main).actionView as SearchView
        search.apply {
            imeOptions = EditorInfo.IME_ACTION_DONE
            isSubmitButtonEnabled = true
            setOnQueryTextListener(object : SearchView.OnQueryTextListener {
                override fun onQueryTextSubmit(p0: String?): Boolean {
                    return false
                }

                override fun onQueryTextChange(p0: String?): Boolean {
                    // My Function
                    return false
                }
            })
        }
        val menuItemSearch = menu.findItem(R.id.action_search_main)
        menuItemSearch.setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
            override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
                Log.wtf("FAB", "HIDE")
                fab.hide()
                return true
            }

            override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
                Log.wtf("FAB", "SHOW")
                fab.show()
                return true
            }
        })

        return super.onCreateOptionsMenu(menu)
    }