在Android中的ScrollView中使用TextView.postDelayed的打字机效果

时间:2012-05-08 18:50:13

标签: android

我正在使用scrollview中的textview创建一个可运行的打字机效果。

这就是我做的......

    final ScrollView sv = new ScrollView(this);
    sv.setLayoutParams(new LinearLayout.LayoutParams((int)display.getWidth()/2, (int)((display.getHeight()/4))*3));
    sv.setPadding(0, px2DP(10), px2DP(10), px2DP(10));

    final CharSequence text = getResources().getText(R.string.longstring);

    final TextView content = new TextView(this);
    content.setBackgroundColor(getResources().getColor(R.color.Black));
    content.setTextColor(getResources().getColor(R.color.White));
    content.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
    content.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

    sv.addView(content);

    content.postDelayed(new Runnable(){
        @Override
        public void run() {
            content.append(text.subSequence(0, index++));
            sv.fullScroll(View.FOCUS_DOWN);     
        }

    },70);

我只得到一个黑盒子,没有文字。

我做错了什么?请帮忙。

PS。我已经找到了解决方案!

1 个答案:

答案 0 :(得分:0)

找到解决方案。我正在使用handler-runnable概念错误。根据Jonas指出的,Runnable只运行一次。因此需要一个循环。

正如此链接所示: How to run a Runnable thread in Android?

答案类似于递归。

无论如何,谢谢你们! Android非常酷。