5

ご覧いただきありがとうございます。チャットアプリケーションを作成しています。ほとんどの場合機能します。私が問題にしているのはスクロールだけです。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>
4

3 に答える 3

16

解決策は、郵便で別のUIメッセージを送信することです。それは間違いなく機能します。ScrollView内のTextViewにテキストを設定/追加した後、以下のコードのようにpost(Runnable)メソッドを介してScrollViewを更新します。

messageView.append(blabla);      
scroller.post(new Runnable() { 
                public void run() { 
                    scroller.smoothScrollTo(0, messageView.getBottom());
                } 
            }); 
于 2011-03-01T13:18:03.987 に答える
10

これを行う最善の方法は、EditTextではなくスクローラーでTextViewを使用すると思います。これは、メッセージを印刷するとユーザーが編集できないためです。このようなものを試してみてください。これはメッセージを印刷する美しい方法です

<ScrollView android:id="@+id/scroller"
        android:layout_width="fill_parent" android:layout_height="fill_parent"

        android:background="#FFFFFF">
        <TextView android:id="@+id/messageView"
            android:layout_height="fill_parent" android:layout_width="fill_parent"
            android:paddingBottom="8dip" android:background="#ffffff"
            android:textColor="#000000" />
    </ScrollView>

自動的に下にスクロールするには、メッセージを押して表示した後にこのメソッドを呼び出します

private void scrollDown() {
        scroller.smoothScrollTo(0, messageView.getBottom());  
}

ここでスクローラーはScrollViewで、messageViewはTextViewです

HTMLのようなものを使用して、異なる色でメッセージを印刷することもできます

messageView.append(Html.fromHtml("<font color='green'><b>("+date+") "+ username +":</b></font><br/>"+ message+"<br/>", null, null));
于 2011-02-24T07:26:42.833 に答える
1

この方法を試してください: EditTextをプログラムでスクロールする

于 2011-02-24T07:05:31.443 に答える