2012-05-08 65 views
0

我使用scrollview中的textview創建了可運行的打字機效果。在Android中的ScrollView中使用TextView.postDelayed的打字機效果

這是我做過什麼......

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。我已經找到了解決方案!

+0

您是否將ScrollView添加到主視圖?另外,如果你在Runnable中執行'sv.invalidate()',該怎麼辦? – Jonas

+0

是的,測試以確保一切安裝正確。嘗試在content.postDelayed(...);':content.setText(「Testing」)之前添加,以確保簡單案例正確顯示。 – kabuko

+0

另請注意,postDelayed只運行一次Runnable。 http://developer.android.com/reference/android/view/View.html#postDelayed(java.lang.Runnable,+long) – Jonas

回答

0

找到解決方案。我正在使用處理程序可運行的概念錯誤。根據Jonas指出,Runnable只運行一次。因此一個循環是必要的。

至於建議此鏈接: How to run a Runnable thread in Android?

答案是類似的東西遞歸。

無論如何謝謝你們! Android非常酷。