久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用戶打字時如何避免 EditText 上的多個觸發器?

How to avoid multiple triggers on EditText while user is typing?(用戶打字時如何避免 EditText 上的多個觸發器?)
本文介紹了用戶打字時如何避免 EditText 上的多個觸發器?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

當用戶輸入 EditText 時,我使用以下代碼執行搜索:

I use the following code to perform search when user types in an EditText :

EditText queryView = (EditText) findViewById(R.id.querybox);
queryView.addTextChangedListener(new TextWatcher() {
  @Override
  public void afterTextChanged(Editable s) {
    triggerSearch(s.toString()); 
  }
  @Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  }
  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count) {
  }
});

但是,當用戶鍵入一個單詞時,這會觸發多次.也就是說,如果用戶鍵入hello",此代碼將觸發 5 次值(h"、he"、hel"、hell"、hello").通常,這會很好,但觸發搜索很昂貴,我不想將資源浪費在沒有多大用處的中間搜索上.我想要的是在用戶開始鍵入后僅觸發某個閾值的偵聽器,或者是某種框架,它在調用 triggerSearch 之前在偵聽器中等待,并且如果在該等待之前觸發了另一個事件, 自行取消.

However, this triggers multiple times when the user is typing a word. That is if the user is typing "hello", this code will trigger 5 times with values ("h", "he" , "hel", "hell", "hello"). Normally, this would be fine but the triggered search is expensive and I don't want to waste resources on intermediate searches that are of no great use. What I want is either a listener that triggers only a certain threshold after the user starts typing, or some kind of framework, that waits in the listener before calling triggerSearch, and if another event is triggered before that wait, cancels itself.

推薦答案

由于找不到合適的事件接口,嘗試觸發延遲搜索.代碼實際上非常簡單和健壯.

Since couldn't find an appropriate event interface, tried triggering a delayed search. The code is actually pretty simple and robust.

private final int TRIGGER_SERACH = 1;
// Where did 1000 come from? It's arbitrary, since I can't find average android typing speed.
private final long SEARCH_TRIGGER_DELAY_IN_MS = 1000;

  private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      if (msg.what == TRIGGER_SERACH) {
        triggerSearch();
      }
    }
  };

 queryView.addTextChangedListener(new TextWatcher() {

   @Override
   public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

   }

   @Override
   public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

   }

   @Override
   public void afterTextChanged(Editable s) {
    handler.removeMessages(TRIGGER_SERACH);
    handler.sendEmptyMessageDelayed(TRIGGER_SERACH, SEARCH_TRIGGER_DELAY_IN_MS);
   });

這篇關于用戶打字時如何避免 EditText 上的多個觸發器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 亚洲免费在线 | 亚洲精品免费看 | 欧美激情久久久 | 日韩精品 电影一区 亚洲 | 亚洲精品国产第一综合99久久 | 亚洲综合在 | 欧美一区二区三区四区视频 | 国产精品久久久久久久久久软件 | 成人激情视频免费观看 | 久久一级 | 国产9 9在线 | 中文 | 精品久久久久久中文字幕 | 激情久久网 | 中文字幕日韩在线观看 | 久久99精品久久久 | 天天操夜夜操 | 国内精品成人 | 一区二区三区av | 日韩一区二区在线视频 | 免费精品| 久久精品成人 | 久久88 | 精品一区免费 | 欧美精品三区 | 国产成人精品一区二区三区视频 | 黄色免费在线观看网站 | 日本一二三区在线观看 | 一区二区国产在线 | 精品videossex高潮汇编 | 久久亚洲春色中文字幕久久久 | 国产成人综合在线 | 久久成人18免费网站 | 国产欧美精品区一区二区三区 | 91亚洲国产成人久久精品网站 | 亚洲国产视频一区二区 | 国产精品久久久久久久久久久免费看 | 一区二区在线免费观看 | 日韩精品在线观看一区二区 | 国产精品乱码一区二三区小蝌蚪 | 综合久久一区 | 国产在线色 |