安卓:自动聊天应用向下滚动EditTextView [英] Android: auto scrolling down the EditTextView for chat apps
问题描述
谢谢你看,
我创建一个聊天应用程序。它工作的大部分。
我有一个问题的唯一事情是滚动。
我使用的EditText 以发布来自服务器的新的消息。
由法
味精=味精+\\ n+ newMsg
EditText.setText(MSG)
我需要新的文本,一旦是根据旧的文本可见,因为它来了。
因此,我认为最好的办法就是自动向下滚动以底部一旦视图被更新。
有没有一种简单的方法来做到这一点?像布局也许?
再次感谢!
code布局
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<按钮
机器人:ID =@ + ID / sendButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_alignParentRight =真
机器人:文字=发送
/>
<的EditText
机器人:ID =@ + ID / MSGBOX
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =FILL_PARENT
机器人:layout_alignParentBottom =真
机器人:layout_toLeftOf =@ + ID / sendButton
机器人:重力=左
机器人:longClickable =假
/>
<的EditText
机器人:ID =@ + ID /客舱
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:编辑=假
机器人:layout_above =@ + ID / MSGBOX
机器人:滚动条=垂直
机器人:重力=左|顶
机器人:isScrollContainer =真
机器人:cursorVisible =假
机器人:longClickable =假
机器人:可点击=假
机器人:自动链接=所有
/>
< / RelativeLayout的>
一个解决方案是通过邮寄发送一个单独的UI消息。
这肯定会工作。
你以后的setText /文本追加到您的TextView的滚动型内
messageView.append(布拉布拉);
scroller.post(新的Runnable(){
公共无效的run(){
scroller.smoothScrollTo(0,messageView.getBottom());
}
});
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>
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:
messageView.append(blabla);
scroller.post(new Runnable() {
public void run() {
scroller.smoothScrollTo(0, messageView.getBottom());
}
});
这篇关于安卓:自动聊天应用向下滚动EditTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!