【问题标题】:How to use TextWatcher when user finished/paused typing?用户完成/暂停输入时如何使用 TextWatcher?
【发布时间】:2015-10-16 09:29:18
【问题描述】:

我遇到了一个优化问题:我想做一个动态的 AutoCompleteTextView,所以我在我的类中实现了“TextWatcher”。我希望每次用户输入并暂停或完成输入时,我都会启动我的数据库请求以获取自动完成列表,所以我的问题是:

我如何知道用户何时完成/暂停在 AutoCompleteTextView 中输入?

实际上,每次用户输入字符时,我的代码都会发出一个请求。因此,对于“Hello”,它将向我的数据库发送 4 个请求,但我需要它在用户完成输入时发送一个包含完整世界“Hello”的唯一请求。

这可能吗?

这是我实际代码的一部分:

public class MainActivity extends AppCompatActivity implements TextWatcher {

public final static String CLUB_NAME = "com.mycompany.myfirstapp.FAV_CLUB";

List<devaddict.footbarcom.modelPOJO.Object> list;
Button searching;
AutoCompleteTextView searchbar_club;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    searching = (Button)findViewById(R.id.buttonseek);
    searchbar_club = (AutoCompleteTextView) findViewById(R.id.searchbar_club);

    searchbar_club.addTextChangedListener(this);
    searchbar_club.setThreshold(3);

}

然后,在同一个“MainActivity”类中,我得到了这些:

@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {

    System.out.println("TEXT CHANGED BY onTextChanged");

}

@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

}

@Override
public void afterTextChanged(final Editable s) {
  //System.out.println("TEXT CHANGED BY afterTextChanged");
}

我认为“afterTextChanged”方法是我真正想要的,但我看不出它和 onTextChanged 有什么区别。

System.out 仅用于测试。

【问题讨论】:

  • 您是否要等到用户在输入第一个单词后输入第一个空格?
  • 这可能是一个很好的解决方案@Droidwala,但有时用户只需键入“PSG”之类的首字母缩略词。

标签: android retrofit autocompletetextview textwatcher


【解决方案1】:

onTextChanged() 是您可以写下逻辑的主要方法。 如果您愿意等待用户输入 2 个或更多单词,您可以编写 onTextChanged() 方法,如下所示:

@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {

 if(count > 4){
     new Handler().postDelayed(new Runnable(){
          public void run(){
               //write db insertion logic here...
        }},700);
   }
}

基本上你会等待 700 毫秒,然后执行一些操作以确保用户可能已经停止输入..

看看有没有帮助!!

P.S:阿里在下面使用计时器的回答也是OP提出的问题的正确方法

【讨论】:

  • 好的,我已经尝试过了,但它并没有解决我的问题。你给我的只是给所有字符治疗之间的延迟。对于“Hello”,它仍然运行 5 次(因为用户修饰符文本 5 次,1 次 / 字母)。我需要用户写“你好”,暂停后,启动我的数据库插入。但是谢谢你给我的东西。
  • 好吧。那么您可以增加计数值,例如 if(count > 4 or 5)
  • @Addict 你试过增加计数吗?通常,我在我的应用程序中使用它来获取自动完成的谷歌地方 api,它可以工作..是的,没有计数逻辑的延迟逻辑毫无价值..
  • 是的,我试过了,但我什至不明白“计数”的含义,文档对此并不了解。
  • 一旦用户在输入一个单词后点击第一个空格,那么 count 的值将变为 0,start 的值将变为 1
【解决方案2】:
    Timer timer;

    @Override
    public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {

    timer = new Timer();
    timer.schedule(new TimerTask()
    {
        @Override
        public void run()
        {
            MainActivity.activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    timer.cancel();
                    //do something

                }
            });
        }

    }, 1000);//after 1 second do whatever you want

}

@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

}

@Override
public void afterTextChanged(final Editable s) {
  //System.out.println("TEXT CHANGED BY afterTextChanged");
}

【讨论】:

  • 这项工作就像一个魅力,在我暂停后,我得到了我需要的单词,但是有一个问题。计时器暂停后,(以您的示例为 1 秒)计时器崩溃。知道为什么吗?你需要 logcat 什么的吗?这里有一个崩溃:pastebinoflogcat 似乎是 timer.cancer() 造成的。但是没有它,我得到与@Droidwala 相同的结果,它只是在结果之间放置时间,但仍然为“Hello”提供 5 个结果。谢谢你的帮助。
  • 这似乎运作良好,它减少了请求的数量,我通过比较新文本是否与旧文本相似来减少它。 @Droidwala 解决方案做同样的事情,它也可以工作。所以两者都是有效的答案。
【解决方案3】:

以下是使用 Kotlin 和协程的方法: 首先在您的 Activity 或 Fragment 中定义一个工作,如下所示:

private var textChangeCountDownJob: Job = Job()

然后像这样添加一个文本观察器:

editText.doAfterTextChanged {
            textChangeCountDownJob.cancel()
            textChangeCountDownJob = lifecycleScope.launch {
                delay(500) // for half a second delay, as an example
                search(it.toString()) // doing your logic here
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    相关资源
    最近更新 更多