0

我有一个TextViewwholayout_width设置为wrap_content. 我使用九个补丁图像作为背景。TextView 的内容会定期刷新(我使用休眠一段时间然后启动处理程序的线程)。经过几次迭代后,出现了一些伪影。我可以看到一些较早消息的部分文本和背景(它的宽度大于当前显示的消息的宽度)。可能是什么问题呢?

public void onCreate{
   ...
   helpField = (TextView) findViewById(R.id.helpField);
   ...
}

private class PeriodicHelp extends Thread{
    private static final int SLEEP_TIME = 4000;
    private static final int NUM_HELP_PHRASES = 5;

    private String getHelp(){
        int pos = randomGenerator.nextInt(NUM_HELP_PHRASES);
        return helpPhrases[pos];
    }

    public void run(){
        int i = 0;
        while(true){
            try {
                Thread.sleep(SLEEP_TIME);
                Log.d(TAG, "periodicHelp " + Integer.toString(i));
                i++; //used just for debugging
                mHandler.post(new Runnable(){
                    public void run() {
                        String help = getHelp();
                        helpField.setText(help);
                    }
                });
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

我开始我的 PeriodicHelp 线程onStart()

4

1 回答 1

0

我设法解决了这个问题。这是代码

public void onCreate{
   ...
   helpField = (TextView) findViewById(R.id.helpField);
   rl = (RelativeLayout) findViewById(R.id.mainView); //this RelativeLayout contains
                                                      //helpField  TextView     
   ...
}

private class PeriodicHelp extends Thread{
private static final int SLEEP_TIME = 4000;
private static final int NUM_HELP_PHRASES = 5;

private String getHelp(){
    int pos = randomGenerator.nextInt(NUM_HELP_PHRASES);
    return helpPhrases[pos];
}

public void run(){
    int i = 0;
    while(true){
        try {
            Thread.sleep(SLEEP_TIME);
            Log.d(TAG, "periodicHelp " + Integer.toString(i));
            i++; //used just for debugging

            rl.postInvalidate();//THIS LINE FIX THE PROBLEM

            mHandler.post(new Runnable(){
                public void run() {
                    String help = getHelp();
                    helpField.setText(help);
                }
            });
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

}

于 2012-12-12T12:05:22.640 回答