1

smackライブラリに基づいてチャットアプリケーションを作成しようとしています。logcatはすべてを正しく送受信するため、私の問題はこのライブラリとは何の関係もないと思います。私の問題は、メッセージを受信して​​も表示されないことです。以下は、私がテキストを表示するために使用している方法です。

public void TextCreating(String message) {

    Log.d("START", "Method Excution started.");

    layout = (LinearLayout) findViewById(R.id.layoutLinear);
    LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    tv = new TextView(this);
    tv.setLayoutParams(lparams);
    tv.setText(message);
    tv.setTextColor(Color.WHITE);
    Log.d("STARTED", "Text color OK.");
    layout.addView(tv);
    Log.d("STARTED", "Text didsplayed.");
}

このコードの平和は、受信したメッセージとして実行されます。これらの受信メッセージは、PacketListenerと呼ばれるリスナーを介して処理されます。この受け取り部分は正常に機能しています。このリスナーでは、上記のTextCreatingメソッドを呼び出すと、tv.setTextColor(Color.WHITE)まで実行されます。最後の行は実行されません。これの理由は何ですか?どうすればこの問題を解決できますか?前もって感謝します :)

編集 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Chat Room"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="Chat History :" />

<LinearLayout
    android:id="@+id/layoutLinear"
    android:layout_width="fill_parent"
    android:layout_height="250dp"
    android:orientation="vertical"
    android:background="#000000"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp" >

</LinearLayout>

<EditText
    android:id="@+id/editTextMessage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/buttonSend"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Send" />

</LinearLayout>
4

4 に答える 4

0

チャット ボックスを作成する代替ソリューションを次に示します。その用途ListView。新着商品として追加message

android:stackFromBottom

true に設定すると、チャット ボックスのようになります。デフォルトでは、スクロールもあります。

それが役に立てば幸い。

于 2012-09-27T04:28:55.043 に答える
0

Android SDK には、Bluetooth を介して通信するサンプル チャット アプリケーションがあります。コードで見ることができます。

を使用しListViewて会話を表示します

この回答がお役に立てば幸いです:)

于 2012-09-27T05:07:25.803 に答える
0

まず、あなたのアイデアと助けに感謝します。コードに Handler を導入することで、これを解決します。UIスレッドですべてを実行しようとしていました。だから私は次のように私のコードを変更しました。

private Handler mHandler = new Handler();
public void TextCreating(final String message) {

    mHandler.post(new Runnable() {
        @Override
        public void run() {             
            layout = (LinearLayout) findViewById(R.id.layoutLinear);
            LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            tv = new TextView(MUCchat.this);
            tv.setLayoutParams(lparams);
            tv.append(message);
            tv.setTextColor(Color.WHITE);
            layout.addView(tv);
        }
    });     
}

このリンクは、これを理解するのに大いに役立ちました。 スレッドから textView を更新する

于 2012-09-27T08:49:25.310 に答える