如何在 textview 上显示最新的附加文本? [英] How can I show the latest append text on textview?

查看:24
本文介绍了如何在 textview 上显示最新的附加文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款聊天应用.每当我提交或接收短信时,我都会将它们附加到聊天框.当列表变长时,我需要向下滚动才能看到它们.如何让它自动滚动到新追加的文本?

I working on a chat app. Whenever I submit or receive a text message, I append them to the chatbox. When the list get longer, I need to scroll down to see them. How can I make it to autoscroll to the newly append text?

<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();
    }
});  

推荐答案

我遇到了同样的问题,这是我正在使用的:

I had the same issue, this is what I'm using:

//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 在哪里:

where mChatBox is:

ScrollView mChatBox;

这篇关于如何在 textview 上显示最新的附加文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆