简体   繁体   中英

Android EditText setText cause softkeyboard to freeze

I am having a problem with the 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).

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. SO you have to find the way of terminating the afterTextChange() logic. For better help please post TextWatcher code too.

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. You should use a boolean to trace whether the event is genrated from onpostexecute or by keyboard input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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