最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android?SearchView搜索控件使用方法詳解

 更新時間:2022年04月18日 14:32:52   作者:清風一杯酒  
這篇文章主要為大家詳細介紹了Android?SearchView搜索控件的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android SearchView搜索控件的具體實現代碼,供大家參考,具體內容如下

方法介紹

  • setQueryHint

設置 Hint 的文字內容

  • setMaxWidth

設置搜索框的最大寬度

  • setSubmitButtonEnabled

是否顯示提交按鈕,默認是false

  • setIconified

搜索框是否展開,false表示展開

  • setIconifiedByDefault

是否鎖定搜索框為展開狀態(tài),false表示鎖定(放大鏡在搜索框外)

  • onActionViewExpanded

鎖定搜索框為展開狀態(tài)

  • onActionViewCollapsed

將當前狀態(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同學","b同學","c同學","d同學","A宿舍","B學校","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("請輸入關鍵詞");
? ? ? mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){

? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onQueryTextSubmit(String p1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ?//點擊搜索按鈕事件
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?//收起鍵盤
? ? ? ? ? ? ? ?mSearchView.clearFocus();
? ? ? ? ? ? ? ?//收起搜索框
? ? ? ? ? ? ? ?mSearchView.onActionViewCollapsed();
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onQueryTextChange(String p1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ?//當搜索框的文字內容發(fā)生變化時調用
? ? ? ? ? ? ? ?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>

效果圖

總結了幾個比較常見的問題,并在網上找到了相應的解決方案,親測有效

問題1:如何去掉下劃線或自定義背景

try { ? ? //--拿到字節(jié)碼 ? ? ?
? ? ? Class<?> argClass = mSearchView.getClass();
? ? ? //--指定某個私有屬性,mSearchPlate是搜索框父布局的名字 ? ? ? ?
? ? ? Field ownField = argClass.getDeclaredField("mSearchPlate");
? ? ? //--暴力反射,只有暴力反射才能拿到私有屬性 ? ? ??
? ? ? ownField.setAccessible(true);
? ? ? View mView = (View) ownField.get(mSearchView);
? ? ? //--設置背景?
? ? ? mView.setBackgroundColor(Color.TRANSPARENT);
? ? } catch (Exception e) {
? ? ? e.printStackTrace();
}

問題2:自定義文字與光標的顏色

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
{
? ?//自定義光標顏色
? ?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:改變圖標渲染顏色為白色

  • 首先用Toolbar代替ActionBar
  • 然后為Toolbar添加屬性
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android ViewPager實現圖片輪播效果

    Android ViewPager實現圖片輪播效果

    這篇文章主要為大家詳細介紹了Android ViewPager實現圖片輪播效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android?webview攔截H5的接口請求并返回處理好的數據代碼示例

    Android?webview攔截H5的接口請求并返回處理好的數據代碼示例

    這篇文章主要給大家介紹了關于Android?webview攔截H5的接口請求并返回處理好的數據的相關資料,通過WebView的shouldInterceptRequest方法,Android可以攔截并處理WebView中的H5網絡請求,需要的朋友可以參考下
    2024-10-10
  • Android自定義View過程解析

    Android自定義View過程解析

    這篇文章主要針對Android自定義View過程進行解析,Android創(chuàng)建自定義的view,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android多功能視頻播放器GSYVideoPlayer開發(fā)流程

    Android多功能視頻播放器GSYVideoPlayer開發(fā)流程

    怎么在Android中實現GSYVideoPlayer視頻播放器?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲
    2022-11-11
  • android開發(fā)設計模式之——單例模式詳解

    android開發(fā)設計模式之——單例模式詳解

    本篇文章主要介紹了android開發(fā)設計模式之——單例模式詳解,具有一定的參考價值,有需要的可以了解一下。
    2016-11-11
  • Android控件之TextView的分析探究

    Android控件之TextView的分析探究

    本篇文章介紹了,Android控件之TextView的分析探究。需要的朋友參考下
    2013-04-04
  • Android中微信小程序支付倒計時功能

    Android中微信小程序支付倒計時功能

    大家在使用微信的時候都注意過微信支付倒計時功能,怎么實現的呢?今天小編給大家分享微信小程序支付倒計時功能實現思路詳解,一起看看吧
    2016-12-12
  • Android PopupWindow實現微信右上角的彈出菜單

    Android PopupWindow實現微信右上角的彈出菜單

    這篇文章主要為大家詳細介紹了Android PopupWindow實現微信右上角的彈出菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android中Fragment管理及重疊問題的解決方法

    Android中Fragment管理及重疊問題的解決方法

    最近做項目碰到了Fragment重疊的問題,后來通過種種方法得以解決了,所以想著總結下這個問題的解決方法,以及Android中Fragment的管理,方便自己也給有需要的朋友們提供以幫助,感興趣的朋友們下面通過這篇文章一起來學習學習吧
    2016-11-11
  • Android中的Service相關全面總結

    Android中的Service相關全面總結

    接下來將介紹Service的種類;Service與Thread的區(qū)別;Service的生命周期;startService 啟動服務;Local與Remote服務綁定等等,感興趣的朋友可以了解下
    2013-01-01

最新評論

隆回县| 增城市| 东至县| 恩平市| 武义县| 望奎县| 拉萨市| 沧州市| 兴和县| 大庆市| 长治市| 杭锦后旗| 南丰县| 广德县| 昭通市| 江达县| 昭平县| 苍山县| 大安市| 新晃| 乐山市| 平邑县| 曲靖市| 苍梧县| 兰西县| 永定县| 渭南市| 五大连池市| 扎鲁特旗| 治多县| 共和县| 兴山县| 兰溪市| 城步| 公安县| 宿州市| 五大连池市| 玛曲县| 肇庆市| 双桥区| 平山县|