티스토리 뷰

 1) 가장 먼저 Client UI를 구현해 보겠습니다
 
: 이렇게 Simple하게 구성해 봤습니다. 
위에 TextView를 두고, 아래에는 EditText Button을 뒀습니다.
원래 안드로이드에서는 채팅기능을 구현할때 ListView로 구현하는게 가장 일반적인데요, 
 '네트워크'에 집중하기 위해 최대한 간소화해서 TextView로 진행해 볼겁니다. 
( 더 채팅같이 보이려면 'ListView'와 '나인패치'를 적용하시면 됩니다!  ) 
 

 

  2) 코드는 이렇습니다 


 따로 어려운 포인트는 없고,
MainActivity에서는 '키패드 처리'를 해줬습니다. 
container layout을 터치하면 hideSoftInputWindow()를 콜했죠. 

 

 

activity_main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.kbpark.blogmultichatclient.MainActivity">
 
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/container"
        android:layout_weight="1"
        >
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/frameLayout">
            <TextView
                android:id="@+id/textView"
                android:listSelector="#00000000"
                android:cacheColorHint="#00000000"
                android:divider="#000000"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:transcriptMode="alwaysScroll"/>
        </FrameLayout>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#2F4F4F"
        android:layout_below="@id/container"
        android:orientation="horizontal"
        android:weightSum="1">
 
        <EditText
            android:hint="메세지를 입력하세요"
            android:id="@+id/inputText"
            android:inputType="textMultiLine"
            android:maxLines="4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.92" />
 
        <Button
            android:id="@+id/sendBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="send"
            android:onClick="onSendBtnClicked" />
 
    </LinearLayout>
</LinearLayout>
 
cs
 

- MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.example.kbpark.blogmultichatclient;
 
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    TextView textView;
    LinearLayout container;
    EditText inputText;
 
    ChatClient client;
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        /**
         * client 연결을 합니다.
         */
        client = new ChatClient(this);
        client.startClient();
 
        inputText = (EditText) findViewById(R.id.inputText);
        textView = (TextView) findViewById(R.id.textView);
        container = (LinearLayout) findViewById(R.id.container);
 
        container.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                hideSoftInputWindow(inputText);
                return true;
            }
        });
 
    }
 
    public void onSendBtnClicked(View v)
    {
 
        String msg = inputText.getText().toString();
        inputText.setText("");
 
        // ChatClient가 메시지를 server로 보낼수 있도록 msg를 날려줍니다.
        client.sendMsg(msg);
    }
 
    public void hideSoftInputWindow(View editView)
    {
        InputMethodManager imm = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE);
 
        imm.hideSoftInputFromWindow
                (editView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
 
    // Client에서 msg를 받아서 뿌리는 메소드입니다.
    public void typeMsg(String msg)
    {
        inputText.setText(msg);
    }
 
}
 
cs

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함