我怎样才能显示在TextView的最新追加的文字? [英] How can I show the latest append text on textview?

查看:813
本文介绍了我怎样才能显示在TextView的最新追加的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个聊天应用程序。 每当我提出或者收到短信,我附上他们的客舱。 当列表变得更长,我需要向下滚动才能看到它们。 我怎样才能使它自动滚动到新添加的文字?

 <滚动型
    机器人:ID =@ + ID / scrollView01
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =100px的>
        <的TextView
            机器人:ID =@ + ID / txtChat
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=
            />
< /滚动型>
 

//

  SendMsg.setOnClickListener(新View.OnClickListener()
{
    公共无效的onClick(视图v)
    {
        字符串名称= txtName.getText()的toString()。
        字符串消息= txtMessage.getText()的toString()。

    如果(message.length()大于0){
            SENDMSG(姓名,邮件);
            字符串myMessage =信息+\ N的;
            tvChat.append(myMessage);
        }
        其他
            Toast.makeText(getBaseContext(),
                请输入姓名和消息。,Toast.LENGTH_SHORT).show();
    }
});
 

解决方案

我有同样的问题,这是我使用的是什么:

  //延迟必须pssed毫秒EX $ P $。为3秒,延迟= 3000
私人无效scrollToBottom(INT延迟){
    //如果我们不叫fullScroll里面一个Runnable,它不滚动到
    //底部,但到第(底 -  1)
    mChatBox.postDelayed(新的Runnable(){
        公共无效的run(){
            mChatBox.fullScroll(ScrollView.FOCUS_DOWN);
        }
    }, 延迟);
}
 

在这里mChatBox是:

 滚动型mChatBox;
 

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

where mChatBox is:

ScrollView mChatBox;

这篇关于我怎样才能显示在TextView的最新追加的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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