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

用戶打字時(shí)如何避免 EditText 上的多個(gè)觸發(fā)器?

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

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

當(dāng)用戶輸入 EditText 時(shí),我使用以下代碼執(zhí)行搜索:

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) {
  }
});

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

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.

推薦答案

由于找不到合適的事件接口,嘗試觸發(fā)延遲搜索.代碼實(shí)際上非常簡(jiǎn)單和健壯.

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);
   });

這篇關(guān)于用戶打字時(shí)如何避免 EditText 上的多個(gè)觸發(fā)器?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
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 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標(biāo)在展開(kāi)的 EditText 中的開(kāi)始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問(wèn)題)
主站蜘蛛池模板: 中日韩毛片 | 超碰91在线 | 久久精品国产99国产 | 九七午夜剧场福利写真 | 国产一区二区久久久 | av免费入口 | 亚洲成人一级 | 四虎成人av | 亚洲精品大片 | 国产97在线看 | 欧美在线综合 | 99这里只有精品视频 | 国产日韩精品一区 | 香蕉大人久久国产成人av | 激情婷婷 | 久久久123 | 国产午夜精品视频 | 91在线观看视频 | 黄色永久免费 | 亚洲成人精品在线 | 欧美精品中文 | 超碰操| 国产视频1区2区 | 国产精品久久久久久久三级 | www.五月婷婷.com| 三区在线 | 精品国产一区二区在线 | 91黄色片免费看 | 国产欧美一区二区三区在线看 | 爱草在线| 婷婷开心激情综合五月天 | 色999视频| 亚洲美女一区 | 精品成人av| 欧美日韩激情 | 久久一区二区免费视频 | 亚洲成人一区二区 | 日韩视频一区在线观看 | 红色av社区 | 久久最新 | 欧美日韩国产精品 |