SearchView自定義&ImageView判斷是否默認(rèn)

SearchView自定義:

xml代碼:
<androidx.appcompat.widget.SearchView
            android:id="@id/search_view"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="@drawable/search_view"
            app:queryHint="提示文字" />
Drawable中xml代碼:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充 -->
    <solid android:color="@color/white" />
    <corners android:radius="5dp" />
</shape>

使用過程中發(fā)現(xiàn)Hint文字不顯示,在代碼中進(jìn)行如下設(shè)置即可:

@BindView(R.id.search_view)
SearchView searchView;

searchView.setIconifiedByDefault(false);

其他:

@BindView(R.id.search_view)
SearchView searchView;
/**
 *取消搜索框的下劃線
 */
searchView.findViewById(R.id.search_plate).setBackground(null);
searchView.findViewById(R.id.submit_area).setBackground(null);
/*
 *設(shè)置字體大小等
 */
EditText editText = searchView.findViewById(R.id.search_src_text);
editText.setTextSize(14);
editText.set……
//搜索框監(jiān)聽
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //點(diǎn)擊搜索執(zhí)行邏輯
                ToastUtils.toast("執(zhí)行搜索…");
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //輸入內(nèi)容改變監(jiān)聽
                ToastUtils.toast("輸入內(nèi)容:"+newText);
                return false;
            }
        });
效果圖

ImageView判斷是否是默認(rèn)圖片:

/**
     * 判斷當(dāng)前的圖片是不是和默認(rèn)圖片相同,可以用來判斷用戶是否選擇了圖片
     */
private boolean isImageEmpty() {
    Drawable.ConstantState dftState = ResourcesCompat.getDrawable(getResources(), R.drawable.default, null).getConstantState();
    Drawable.ConstantState state = imageView.getDrawable().getConstantState();
    return dftState.equals(state);
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容