Study

안드로이드 스튜디오 기초

Devailean 2021. 2. 24. 17:04
  • MainActivity.java
  • activity_main.xml
  • Activity : What is shown on the screen.
  • Intent : to initiate new activity and pass data.
  • fragment : miniature Activity.
  • AndroidManifest.xml

 

 

MainActivity.java

 

- Main activity is presented to the user when the app is launched. The main activity can then start other activities to perform different actions.

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

 

String name = ((Button)v).getText().toString();
i.putExtra("NAME", name);
//name이라는 String을 NAME이라는 변수(비유: 수트케이스)에 넣어서 다음 인텐트에게 전달

activity_main.xml - 레이아웃, 출력 화면 설정

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

버튼, 텍스트 등의 attributes / properties -> 이 xml 파일에서 정의됨

ex) text, id -> attribute

 

id attribute -> MainActivity에서 불러옴

Onclick시 toggle하게 하려면

<button(id)

android:onclick="toggle"

이런식으로 하면 되고, toggle 메서드는 MainActivity.java에 정의

 


 

method - section of code.

- public void toggle (parameter){section of code}

//code to toggle, void: returns nothing

 // v는 view 클래스의 객체(인스턴스), 여기선 버튼임
 public void toggle (View v){
 //code
	int x = v.getId();
	v.setEnabled(false);
 }

Concept : make sure to put (View v) as a parameter.

It(v) doesn't really represent specific button but all.

by using v.getId, you can then write a code for specific method.

CAUTION: You have to import android.view.View to use View v.

 

What it VIEW then? check below

developer.android.com/reference/android/view/View

 

View  |  Android 개발자  |  Android Developers

 

developer.android.com

Layout's like box and Views are what's inside that box.

 

public void disable(View v) {
        v.setEnabled(false);
        Button b = (Button) v;
        b.setText("Disabled");
        }

여기서 v는 VIEW 타입의 오브젝트

이 객체를 Button으로 형변환(cast)하여 b라는 버튼 변수에 할당해야함.

cast하는 이유는 setText를 View 오브젝트에 사용할 수 없기 때문.

findViewByID(R.id.button11).setEnabled(false); //works
findViewById(R.id.button11).setText("new disabled") // doesn't work
((button)findViewById(R.id.button11)).setText("new disabled") // works

 

 

findViewById Method

public void x(View view){
	textView t = findViewById(R.id.text);
    //findViewById is a method to access different elements and objects on the screen(activity).
    t.setText("value");
    }

 

 

Toast (import android.widget.Toast) - Alert

Toast.makeText(this, "Alert", Toast.LENGTH_LONG).show();
//this -> MainActivity
//"" -> text to show
// Toast.LENGTH_LONG -> for longer duration

Debugging

Log.d(tag,msg) - import android.util.Log;

Log.d("success","It's working fine")

 


 

AndroidManifest.xml

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <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/Theme.MyApplication">
        <activity android:name=".SettingsActivity"></activity>
        <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>

<activity> </activity> 사이의 <intent-filter> <intent-filter>는 앱의 첫 번째(메인) 액티비티를 결정.

<activity android:name=".MainActivity" android:label="Home">
//android:label은 액티비티 이름을 지정. 메인 액티비티 라벨을 지정하면 앱 이름도 바뀜.

앱 이름을 변경하지 않으려면 해당 자바 파일 안에 setTitle("")을 이용할 수 있다.

그래서 AndroidManifest에서 바꾸지 말고 자바 코드에서 바꾸자.

 

android:parentActivityName=".MainActivity"
// 상위 액티비티 설정, 뒤로가기 대상