【问题标题】:How can I show the latest append text on textview?如何在 textview 上显示最新的附加文本?
【发布时间】:2010-09-10 12:13:10
【问题描述】:

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

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

【问题讨论】:

    标签: android append textview


    【解决方案1】:

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

    //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;
    

    【讨论】:

    • 别忘了 -> mChatBox = (ScrollView)findViewById(R.id.scrollView);
    • 就像其他 android 元素一样 :) 这就是为什么我没有指定它,但感谢发布它,也许它可以帮助某人。
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 2016-04-20
    相关资源
    最近更新 更多