原创

为什么我无法删除处理程序中运行的 EditText 上的文本?

温馨提示:
本文最后更新于 2024年04月12日,已超过 41 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

Here's my code I have within an Activity (API Level 30):

EditText loggerText;
final Handler handlerLogger = new Handler(Looper.getMainLooper());
final int delayHandlerLogger = 200; 

public void syncSongs() {
    handlerLogger.removeCallbacksAndMessages(null);
    loggerText.setText("");
    loggerText.post(this::processLogger);
}

public void processLogger() {
    handlerLogger.postDelayed(new Runnable() {
        public void run() {
            if (!isQueueEmpty()) {
                String loggerLine = getQueueLastString;
                String textCheck = loggerText.getText().toString();
                loggerText.append(loggerLine + "\n");
            }
            handlerLogger.postDelayed(this, delayHandlerLogger);
        }
    }, delayHandlerLogger);
}

When I call syncSongs() from public boolean onOptionsItemSelected(@NonNull MenuItem item) method (which should erase text with setText("") before post), first time it enters on if (!isQueueEmpty()) { statement, textCheck (i.e. the text from loggerText, which should be empty, due to setText("")) is not "".

Why? Am I missing somethings? What's not correct to deal with it on this async process?

It seems loggerText Is a duplicate object within Runnable, because when I switch Activity and check it onResume/onStart, text is empty, while within Runnable remains the appended text on previous iteration (can't get this).

正文到此结束
热门推荐
本文目录