如何在textview上显示最新的附加文本?

时间:2010-09-10 12:13:10

标签: android append 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();
    }
});  

1 个答案:

答案 0 :(得分: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;