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>
使用過(guò)程中發(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)圖片相同鱼冀,可以用來(lái)判斷用戶是否選擇了圖片
*/
private boolean isImageEmpty() {
Drawable.ConstantState dftState = ResourcesCompat.getDrawable(getResources(), R.drawable.default, null).getConstantState();
Drawable.ConstantState state = imageView.getDrawable().getConstantState();
return dftState.equals(state);
}