android searchview 簡單使用


設置樣式 drawable    bg_search/xml
android:background="@drawable/bg_search"
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!--這里是Linearlayout布局的背景顏色-->
    <solid android:color="@color/white" />
    <!--這里是設置邊框線的粗細和顏色-->
    <stroke
        android:width="2dp"
        android:color="@color/el_warning" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
    <!--角的話,里邊有參數可以單獨設置-->
    <corners android:radius="20dp" />

</shape>
下面的處理不顯示 icon 和 下划線  這里使用的時android
設置左側的icon 這里設置為不顯示左側icon  api22 以下無效果
android:searchIcon="@null"    

下面這兩項處理不顯示下划線   api21 以下無效果
android:queryBackground="@null"
android:submitBackground="@null"

 androidx 使用以下方式

app:searchIcon="@null"
app:queryBackground="@null"
app:submitBackground="@null"

 簡單使用

search_view.requestFocus()
        search_view.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextChange(newText: String?): Boolean {
                Log.d(tag,"textChange=$newText")
                return false
            }

            override fun onQueryTextSubmit(query: String?): Boolean {
                Log.d(tag,"textSubmit=$query")
                if(query.isNullOrBlank()){
                    Toast.makeText(this@CourseSearch,"請輸入搜索條件",Toast.LENGTH_SHORT).show()
                    return false
                }

                if(query.length < 2 || query.length > 20){
                    Toast.makeText(this@CourseSearch,"搜索條件2-20位字符",Toast.LENGTH_SHORT).show()
                    return false
                }
                //查詢處理代碼
                return false
            }
        })

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM