1. 程式人生 > >android searchView 去掉預設下劃線 改為圓角搜尋框

android searchView 去掉預設下劃線 改為圓角搜尋框

     客戶要實現一個圓角的搜尋框,我想著5分鐘就可以搞定了,於是開始碼

1、先定義搜尋框

  <SearchView
        android:id="@+id/sv_cus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
        android:background="@drawable/searchview_line"
        android:queryHint="請輸入姓名" />

2.然後定義一個資原始檔 searchview_line.xml 來設定搜尋框的樣式
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- 表示shape的四個角的角度。只適用於矩形shape,這裡的角度是指圓角的程度 -->
    <corners android:radius="50dp" />

    <!-- 這個標籤表示純色填充,通過android:color即可指定shape中填充的顏色 -->
    <solid android:color="@color/white" />

    <!-- Shape的描邊,下面指定了描邊的寬度和描邊的顏色 -->
    <stroke
        android:width="1dp"
        android:color="#d9d9d9" />

</shape>

3.在後臺獲取到搜尋框,並定義搜尋的監聽
svCustomer = (SearchView) findViewById(R.id.cus);
   // 設定搜尋文字監聽
        svCustomer.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 當搜尋內容改變時觸發該方法
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)){
//                    mListView.setFilterText(newText);
                }else{
                    DataKit.searchParams.setSearchKey("");
                    search();
                }
                return false;
            }
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                if (svCustomer != null) {

                    // 得到輸入管理物件
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        // 這將讓鍵盤在所有的情況下都被隱藏,但是一般我們在點選搜尋按鈕後,輸入法都會乖乖的自動隱藏的。
                        imm.hideSoftInputFromWindow(svCustomer.getWindowToken(), 0); // 輸入法如果是顯示狀態,那麼就隱藏輸入法
                    }
                    DataKit.searchParams.setSearchKey(queryText);
                    svCustomer.clearFocus(); // 不獲取焦點
                    search();
                }
                return true;
            }
        });

結果效果竟然是圓角與下劃線並存,雖然搜尋功能實現了,但樣式好醜啊


於是我就去查搜尋框的屬性,也沒找到它有設定下劃線這個屬性呀,就去百度了一下,原來需要動態改變一下

        if (svCus != null) {


            try {        //--拿到位元組碼
                Class<?> argClass = svCustomer.getClass();
                //--指定某個私有屬性,mSearchPlate是搜尋框父佈局的名字
                Field ownField = argClass.getDeclaredField("mSearchPlate");
                //--暴力反射,只有暴力反射才能拿到私有屬性
                ownField.setAccessible(true);
                View mView = (View) ownField.get(svCus);
                //--設定背景
                mView.setBackgroundResource(R.drawable.searchview_line);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

然後去掉佈局檔案中設定的那個 背景

  <SearchView
        android:id="@+id/sv_cus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
 //這一行去掉啦
        android:queryHint="請輸入姓名" />
效果就出來啦