(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "ca-pub-8242763509535969", enable_page_level_ads: true }); 'code/안드로이드' 카테고리의 글 목록 :: 깜냥깜냥
android

이클립스는 javaproject를 만들어 주시고 클래스를 만들 때 주의 해야 할 것은 패키지 이름을 'org.androidtown.socket'으로 해주세요.

MainActivitiy.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
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
 
public class MainActivity extends AppCompatActivity {
    EditText input01;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        input01 = (EditText) findViewById(R.id.input01);
 
        Button button01 = (Button) findViewById(R.id.button01);
        button01.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String addr = input01.getText().toString().trim();
 
                ConnectThread thread = new ConnectThread(addr);
                thread.start();
            }
        });
 
    }
 
    class ConnectThread extends Thread {
        String hostname;
 
        public ConnectThread(String addr) {
            hostname = addr;
        }
        public void run() {
            try {
                int port = 5001;
 
                Socket sock = new Socket(hostname, port);
                ObjectOutputStream outstream = new ObjectOutputStream(sock.getOutputStream());
                outstream.writeObject("Hello AndroidTown on Android");
                outstream.flush();
 
                ObjectInputStream instream = new ObjectInputStream(sock.getInputStream());
                String obj = (String) instream.readObject();
 
                Log.d("MAinActivity", "서버에서 받은 메시지: " + obj);
 
                sock.close();
 
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
 

 

activitiy_main.xml

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
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hyosin.chatt.MainActivity">
 
    <TextView
        android:id="@+id/text01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Click the button to connect the socket. Please check the log."
        tools:context=".MainActivity"/>
 
    <Button
        android:id="@+id/button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/text01"
        android:text="Socket connection"
        android:textSize="20dp"
        android:textStyle="bold"
        tools:context=".MainActivity" />
 
    <EditText
        android:id="@+id/input01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/button01"
        android:hint="연결할 소켓 서버 IP"
        android:text="000.000.000.00"
        android:textSize="16dp"
        android:textStyle="bold"
        tools:context=".MainActivity" />
 
</LinearLayout>
 


AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
    package="com.example.hyosin.chatt">
 
    <uses-permission android:name="android.permission.INTERNET" />
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

 


'code > 안드로이드' 카테고리의 다른 글

[안드로이드] 버튼에 그라데이션 주기!  (0) 2017.04.20
values 폴더 안에 color를 만들거나 colors 안에 밑에 색을 입력을 해주세요! 참고로 values 폴더 파일을 만들려면 values에서 오른쪽 마우스 클릭 후
new->XML->Values XML File에서 만들어주세요.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 색 아무거나 써도 됩니다~-->
    <color name="pressed1">#ff5959</color>
    <color name="pressed2">#524fff</color>
 
    <color name="focused1">#ffe924</color>
    <color name="focused2">#8bbee8</color>
 
    <color name="default1">#f044e7</color>
    <color name="default2">#3676f8</color>
</resources>
 
 



색은 일단 #000000으로 잡고 번호선? 옆에 정사각형으로 그 색이 나와요. 그 사각형을 클릭하시고 변경해주셔도 괜찮습니다~



drawable->new->Drawable resource File




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
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="@color/pressed1"
                android:endColor="@color/pressed2"
                android:angle="270" /><!--angle은 각도 입니다~ 0 45 90 180 270 360-->
            <corners
                android:radius="3dp" /><!-- 여기는 모서리 부분!-->
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" /><!--padding은 margin이랑 비슷한데 안쪽 여백을 정해줍니다-->
            <!--stroke
                android:color="@color/stroke"
                android:width="1pt"/--><!-- 여기는 테두리 부분!-->
        </shape>
    </item>
    <item android:state_focused="true" >
        <shape>
            <gradient
                android:startColor="@color/focused1"
                android:endColor="@color/focused2"
                android:angle="90" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
            <!--stroke
                  android:color="@color/stroke"
                  android:width="1pt"/-->
        </shape>
    </item>
 
    <item>
        <shape>
            <gradient
                android:startColor="@color/default3"
                android:endColor="@color/default4"
                android:angle="270" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
            <!--stroke
                android:color="@color/stroke"
                android:width="1pt"/-->
        </shape>
    </item>
</selector>


pressed는 눌렀을 때 나오는 버튼, focused는 초점이 맞추어졌을 때 맨 밑은 그냥 화면에 보이는, 즉 버튼을 눌리지 않았을 때 보이는 버튼입니다.


마지막으로 버튼에 "@drawable/selector" 추가하면 완성!

1
2
3
4
5
6
 <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="85dp"
            android:layout_weight="0.25"
            android:background="@drawable/selector"/>

 


+ Recent posts