gpt4 book ai didi

android - 如何在 textview 上显示最新的追加文本?

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:16 26 4
gpt4 key购买 nike

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

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

最佳答案

我有同样的问题,这就是我正在使用的:

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

关于android - 如何在 textview 上显示最新的追加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3684489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com