프로그래밍

안드로이드 스튜디오 시작하기

안드로이드 스튜디오
반응형

(안드로이드 스튜디오 애뮬레이터 실행법, 텍스트 뷰, 버튼 사용법)

 

activity_main.xml

<?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="match_parent"
        android:layout_height="match_parent" //부모에 맞춤
        android:gravity="center" // 가운데 정렬
        android:text="Hello World!" //나타나는 문자
        android:textSize="30dp"/> // 나타나는 문자 크기

</LinearLayout> //수평 정렬 레이어

MainActivity.java - 지정할 동작이 없으므로 생략

실행 화면


안드로이드 스튜디오 애뮬레이터 처음 실행 방법

 

저 부분을 눌러 줍니다
AVD Manager을 켭니다
좌측 하단의 create virtual device클릭
마음에 드는 디바이스 선택
마음에 드는 버전도 선택
이름까지 지정해주면 디바이스 설정 완료

- 저 버튼 클릭 또는 shift+f10누르면 실행


xml 파일과 java파일의 연결

 

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button" //java에서 물러오는 아이디
        android:layout_gravity="center" //버튼을 가운데로 보낸다
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" //내용에 맞춘 크기 지정
        android:text="Button" />

    <TextView
        android:id="@+id/textview" //java에서 물러오는 아이디
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1" //가중치 1
        android:text="HELLO!"
        android:textSize="30dp" />

</LinearLayout>
package com.example.practice;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button bnt;
    TextView textView; //버튼과 텍스트 뷰 선언

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bnt = findViewById(R.id.button);
        textView = findViewById(R.id.textview); //xml의 아이디를 이용해 선언된 버튼과 텍스트뷰를 지정해줌

        bnt.setOnClickListener(new View.OnClickListener() {
            @Override //버튼 클릭시 작동할 내용 선언
            public void onClick(View view) {
                if (bnt.getText()=="Button")
                {
                    textView.setText("clicked");
                    bnt.setText("clicked");
                    Toast.makeText(getApplicationContext(),"버튼 클릭",Toast.LENGTH_SHORT).show();
                    // 토스트 메세지 : 잠깐 뜨고 사라지는 알림? 같은거
                }
                else
                {
                    textView.setText("HEllO!");
                    bnt.setText("Button");
                }
            }
        });
    }
}

실행 결과
실행 결과

반응형