簡體   English   中英

Android EditText setText導致軟鍵盤凍結

[英]Android EditText setText cause softkeyboard to freeze

我對EditText有問題。

我已經實現了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));
    }
}

高亮代碼在這里

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

您必須編寫終止TextWatcher的afterTextChange()方法的邏輯,因為每當文本更改后,將調用afterTextChange(),並且每次調用afterTextChange()時,您都將突出顯示AsyncTask中的某些特定關鍵字,然后再次在onPostExecute()中被調用,它將再次setText()。 因此,您必須找到終止afterTextChange()邏輯的方法。 為了獲得更好的幫助,也請發布TextWatcher代碼。

這是由於以下原因:一旦您在onpostexecute中設置了文本,textchange事件就會再次觸發,並再次進入異步任務,從而進入無限循環。 您應該使用布爾值來跟蹤事件是從onpostexecute生成還是通過鍵盤輸入生成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM