Android:自动向下滚动聊天应用程序的 EditTextView
谢谢你的寻找, 我正在创建一个聊天应用程序。它在大多数情况下都有效。 我唯一遇到的问题是滚动。 我使用 EditText 从服务器发布新消息。
通过方法,
msg = msg + "\n" + newMsg
EditText.setText(msg)
我需要使旧文本下的新文本一出现就可见。
所以我认为最好的方法是在视图更新后立即自动向下滚动到底部。
有没有简单的方法可以做到这一点?也许就像布局一样?
再次感谢!
布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="send"
/>
<EditText
android:id="@+id/msgBox"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/sendButton"
android:gravity="left"
android:longClickable="false"
/>
<EditText
android:id="@+id/chatBox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:editable="false"
android:layout_above="@+id/msgBox"
android:scrollbars="vertical"
android:gravity="left|top"
android:isScrollContainer="true"
android:cursorVisible="false"
android:longClickable="false"
android:clickable="false"
android:autoLink="all"
/>
</RelativeLayout>
Thank you for looking,
I am creating a chat application. It works for the most part.
The only thing I have a problem with is the scrolling.
I use EditText to publish the new message from the server.
by method
msg = msg + "\n" + newMsg
EditText.setText(msg)
I need to make the new text that is under the old text visible as soon as it comes.
So I think best way is to auto scroll down to the bottom as soon as the view is updated.
Is there an easy way to do that? like in layout maybe?
Thanks again!
code for layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="send"
/>
<EditText
android:id="@+id/msgBox"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/sendButton"
android:gravity="left"
android:longClickable="false"
/>
<EditText
android:id="@+id/chatBox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:editable="false"
android:layout_above="@+id/msgBox"
android:scrollbars="vertical"
android:gravity="left|top"
android:isScrollContainer="true"
android:cursorVisible="false"
android:longClickable="false"
android:clickable="false"
android:autoLink="all"
/>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案是通过 post 发送单独的 UI 消息。
那肯定会起作用。
在 ScrollView 内设置文本/将文本附加到 TextView 后
通过 post(Runnable) 方法更新 ScrollView,如下代码:
A solution is to send a seperate UI message via post.
That will definitely work.
After you setText/append text to your TextView inside the ScrollView
update the ScrollView via the post(Runnable) method like the code below:
我认为最好的方法是使用带有滚动条的 TextView 而不是 EditText,因为一旦消息打印用户就无法编辑它。尝试这样的方法,这是打印消息的漂亮方法
,并且要在将消息推送到视图后自动向下滚动,请调用此方法
这里滚动器是 ScrollView,messageView 是 TextView
您还可以使用 HTML 打印不同颜色的消息,例如
I think best way to do this use TextView with scroller rather than EditText because once message print user can't edit it. Try something like this, This is beautiful way to print message
And to scroll automatically down call this method after pushing message to view
Here scroller is ScrollView and messageView is TextView
You can also print message with differnt color using HTML like
试试这个方法:
以编程方式滚动 EditText
Try this method:
Programatically scrolling an EditText