Android - Basic

2020. 9. 20. 20:22Android

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
 
</LinearLayout>
 
//------------------------------java code------------------------
package com.example.and0831;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        Button btn=findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this"Button Click", Toast.LENGTH_SHORT).show();
            }
        });
 
    }
}
cs

 

어플 전체의 레이아웃을 LinearLayout으로 바꾸고 TextView와 Button을 부착해서 간단한 화면 구성을 해보았다.

그리고 Button에 OnClickListener 를 부착해서 클릭했을 때 Toast 즉 간단한 문자 출력을 실행해 보았다.

Java를 하다 보니 Java 코드 쪽은 익숙하고 Layout 은 Java.Swing 에서 해보았던 GUI를 하는 것과 흡사해서 

어려운 건 없었다. 문법만 익힌 다면 금방 적응 할수있을 것 같다.

'Android' 카테고리의 다른 글

Android - Intent  (0) 2020.09.20
Android - ClickListener  (0) 2020.09.20
Android - findViewById,xml에 String 적용  (0) 2020.09.20
Android - Rating Bar,Seek Bar  (0) 2020.09.20