简体   繁体   English

Android EditText setText导致软键盘冻结

[英]Android EditText setText cause softkeyboard to freeze

I am having a problem with the EditText . 我对EditText有问题。

I have implemented a TextWatcher and I check every time in afterTextChanged to highlight some specific keywords in an AsyncTask and set the text in onPostExcute (I only touch the UI here), but the soft keyboard freeze when setText was called in onPostExcute (the app didn't freeze). 我已经实现了TextWatcher并且每次都在afterTextChanged进行检查,以突出显示AsyncTask中的某些特定关键字,并在onPostExcute设置文本(我仅在此处触摸UI),但是当在onPostExcute中调用setText时,软键盘冻结(该应用没有不会冻结)。

public class AsyncHighLight extends AsyncTask<String,String,String>
{
    @Override
    protected String doInBackground(String[] p1){
        return SyntaxHighlighter.getInstance(MainActivity.this).highlight(p1[0]);
    }

    @Override
    protected void onPostExecute(String result){
        et.setText(Html.fromHtml(result));
    }
}

The highlight code here 高亮代码在这里

public String highlight(String s){
    String newString = s;
    newString = newString.replace("\n","<br>");
    for (int i = 0 ; i < currentLang.keyword.length ; i ++){
        newString = newString.replace(currentLang.keyword[i],warpColorTag(currentLang.keyword[i]));
    }
    return newString;
}

You have to write the logic of terminate the afterTextChange() method of TextWatcher as whenever the text got change afterTextChange() will gets called and every time afterTextChange() gets called you highlight some specific keywords in an AsyncTask and again in onPostExecute() will gets called and it will setText() again. 您必须编写终止TextWatcher的afterTextChange()方法的逻辑,因为每当文本更改后,将调用afterTextChange(),并且每次调用afterTextChange()时,您都将突出显示AsyncTask中的某些特定关键字,然后再次在onPostExecute()中被调用,它将再次setText()。 SO you have to find the way of terminating the afterTextChange() logic. 因此,您必须找到终止afterTextChange()逻辑的方法。 For better help please post TextWatcher code too. 为了获得更好的帮助,也请发布TextWatcher代码。

This is due to tha reason that as soon as you settext in onpostexecute, textchange event fires again and it goes into async task again thus it goes into a infinite loop. 这是由于以下原因:一旦您在onpostexecute中设置了文本,textchange事件就会再次触发,并再次进入异步任务,从而进入无限循环。 You should use a boolean to trace whether the event is genrated from onpostexecute or by keyboard input 您应该使用布尔值来跟踪事件是从onpostexecute生成还是通过键盘输入生成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM