2010-09-10 36 views
3

我在聊天應用程序工作。 每當我提交或接收短信時,我都會將它們附加到聊天室。 當列表變長時,我需要向下滾動才能看到它們。 如何使它自動滾動到新添加的文本?如何在textview上顯示最新的附加文本?

<ScrollView 
    android:id="@+id/scrollView01" 
    android:layout_width="fill_parent" 
    android:layout_height="100px" > 
     <TextView 
      android:id="@+id/txtChat" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="" 
      />  
</ScrollView> 

//

SendMsg.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View v) 
    {    
     String name = txtName.getText().toString(); 
     String message = txtMessage.getText().toString(); 

    if (message.length() > 0) { 
      sendMsg(name, message); 
      String myMessage = message + "\n"; 
      tvChat.append(myMessage); 
     } 
     else 
      Toast.makeText(getBaseContext(), 
       "Please enter both name and message.", Toast.LENGTH_SHORT).show(); 
    } 
}); 

回答

8

我有同樣的問題,這是我使用的是什麼:

//delay must be expressed in milliseconds. For 3 seconds, delay = 3000 
private void scrollToBottom(int delay) { 
    // If we don't call fullScroll inside a Runnable, it doesn't scroll to 
    // the bottom but to the (bottom - 1) 
    mChatBox.postDelayed(new Runnable() { 
     public void run() { 
      mChatBox.fullScroll(ScrollView.FOCUS_DOWN); 
     } 
    }, delay); 
} 

其中mChatBox是:

ScrollView mChatBox; 
+0

和Don」忘記 - > mChatBox =(ScrollView)findViewById(R.id.scrollView); – conandor 2010-09-10 13:19:24

+0

就像其他的Android元素:)這就是爲什麼我沒有指定它,但感謝發佈它,也許它有助於某人。 – Maragues 2010-09-13 06:45:28

相關問題