網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android SearchView搜索控件的具體實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
方法介紹
- setQueryHint
設(shè)置 Hint 的文字內(nèi)容
- setMaxWidth
設(shè)置搜索框的最大寬度
- setSubmitButtonEnabled
是否顯示提交按鈕,默認(rèn)是false
- setIconified
搜索框是否展開,false表示展開
- setIconifiedByDefault
是否鎖定搜索框為展開狀態(tài),false表示鎖定(放大鏡在搜索框外)
- onActionViewExpanded
鎖定搜索框為展開狀態(tài)
- onActionViewCollapsed
將當(dāng)前狀態(tài)切換為收縮狀態(tài)
- setImeOptions
改變軟鍵盤的右下角的回車鍵值
EditorInfo.IME_ACTION_SEARCH
表示右下角為搜索
使用方法
<?xml version="1.0" encoding="utf-8"?> <LinearLayout ? ?xmlns:android="http://schemas.android.com/apk/res/android" ? ?android:layout_width="match_parent" ? ?android:layout_height="match_parent"> ? ?<ListView ? ? ? android:layout_height="match_parent" ? ? ? android:layout_width="match_parent" ? ? ? android:id="@+id/mainListView1"/> </LinearLayout>
java代碼
public class MainActivity extends AppCompatActivity
?{
? ?private ListView mListView;
? ?private ArrayAdapter mArrayAdapter;
? ?private String[]data={"a同學(xué)","b同學(xué)","c同學(xué)","d同學(xué)","A宿舍","B學(xué)校","C食堂","D教室","AA制"};
? ?private SearchView mSearchView;
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? super.onCreate(savedInstanceState);
? ? ? setContentView(R.layout.activity_main);
? ? ??
? ? ? mArrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,data);
? ? ? mListView=findViewById(R.id.mainListView1);
? ? ? mListView.setAdapter(mArrayAdapter);
? ?}
? ?@Override
? ?public boolean onCreateOptionsMenu(Menu menu)
? ?{
? ? ? getMenuInflater().inflate(R.menu.menu,menu);
? ? ? MenuItem MenuItem=menu.findItem(R.id.action_search);
? ? ? mSearchView=(SearchView) MenuItemCompat.getActionView(MenuItem);
? ? ??
? ? ? mSearchView.setQueryHint("請輸入關(guān)鍵詞");
? ? ? mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onQueryTextSubmit(String p1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ?//點擊搜索按鈕事件
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?//收起鍵盤
? ? ? ? ? ? ? ?mSearchView.clearFocus();
? ? ? ? ? ? ? ?//收起搜索框
? ? ? ? ? ? ? ?mSearchView.onActionViewCollapsed();
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onQueryTextChange(String p1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ?//當(dāng)搜索框的文字內(nèi)容發(fā)生變化時調(diào)用
? ? ? ? ? ? ? ?mArrayAdapter.getFilter().filter(p1);
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ? }
? ? ? ? ?});
? ? ??
? ? ? return super.onCreateOptionsMenu(menu);
? ?}
}
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" ? ?xmlns:app="http://schemas.android.com/apk/res-auto"> ? ?<item ? ? ? android:id="@+id/action_search" ? ? ? app:actionViewClass="android.support.v7.widget.SearchView" ? ? ? app:showAsAction="ifRoom" /> ? ?<item ? ? ? android:id="@+id/action_settings" ? ? ? app:showAsAction="never"/> ? ? </menu>
效果圖
總結(jié)了幾個比較常見的問題,并在網(wǎng)上找到了相應(yīng)的解決方案,親測有效
問題1:如何去掉下劃線或自定義背景
try { ? ? //--拿到字節(jié)碼 ? ? ?
? ? ? Class<?> argClass = mSearchView.getClass();
? ? ? //--指定某個私有屬性,mSearchPlate是搜索框父布局的名字 ? ? ? ?
? ? ? Field ownField = argClass.getDeclaredField("mSearchPlate");
? ? ? //--暴力反射,只有暴力反射才能拿到私有屬性 ? ? ??
? ? ? ownField.setAccessible(true);
? ? ? View mView = (View) ownField.get(mSearchView);
? ? ? //--設(shè)置背景?
? ? ? mView.setBackgroundColor(Color.TRANSPARENT);
? ? } catch (Exception e) {
? ? ? e.printStackTrace();
}
問題2:自定義文字與光標(biāo)的顏色
int searchPlateId = mSearchView.getContext().getResources()
? ?.getIdentifier("android:id/search_plate", null, null);
View searchPlate = mSearchView.findViewById(searchPlateId);
if (searchPlate != null)
{
? ?int searchTextId = searchPlate.getContext().getResources()
? ?.getIdentifier("android:id/search_src_text", null, null);
? ?//自定義文本顏色
? ?TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
? ?if (searchText != null)
? ?{
? ? ? searchText.setTextColor(Color.WHITE);
? ? ? searchText.setHintTextColor(Color.WHITE);
? ?}
try
{
? ?//自定義光標(biāo)顏色
? ?Field mCursorDrawableRes=TextView.class.getDeclaredField("mCursorDrawableRes");?
? ?mCursorDrawableRes.setAccessible(true);?
? ?mCursorDrawableRes.set(searchText, R.drawable.cursor_color);?
}
catch (Exception e){}
}
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> ?? ?<solid ?? ??? ?android:color="#FFFF0000"/> ?? ?<size ?? ??? ?android:width="1dp"/> </shape>
問題3:改變圖標(biāo)渲染顏色為白色
- 首先用Toolbar代替ActionBar
- 然后為Toolbar添加屬性
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
原文鏈接:https://blog.csdn.net/weixin_45263548/article/details/96897342
相關(guān)推薦
- 2021-12-12 c++虛函數(shù)與虛函數(shù)表原理_C 語言
- 2022-06-12 QT?.pro文件使用解析_C 語言
- 2023-01-18 C#實現(xiàn)設(shè)置電腦顯示器參數(shù)_C#教程
- 2022-12-05 Android開發(fā)InputManagerService創(chuàng)建與啟動流程_Android
- 2022-06-25 Qt一個進(jìn)程運行另一個進(jìn)程的實現(xiàn)方法_C 語言
- 2022-08-08 python中Pytest常用的插件_python
- 2023-06-13 react拖拽組件react-sortable-hoc的使用_React
- 2022-09-30 Nginx使用ngx_http_upstream_module實現(xiàn)負(fù)載均衡功能示例_nginx
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支