본문 바로가기

AndroidStudio

[Android Studio] 버튼 onClick 기초

AndroidManifest == web.xml
(activity 호출)       /   (Servlet 호출)
activity_main.xml   /     jsp

이런 느낌이다.

 

AndroidManifest.xml을 살펴보면

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApp"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity8"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

이런 형식인데 activity 태그 안에 android:name="실행할 자바파일"을 설정해 주면 된다.

 

예제1) 버튼 눌렀을 때 Good Morning, Good Afternoon, Good Evening이 바뀌며 출력 되게 하기

 

public class MainActivity extends AppCompatActivity {

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

        final TextView text = findViewById(R.id.tv);
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String a = (String) text.getText();
                if(a.equals("Good Evening")){
                    text.setText("Good Morning");
                }else if(a.equals("Good Afternoon")){
                    text.setText("Good Evening");
                } else{
                    text.setText("Good Afternoon");
                }

            }

        });


    }

여기서

setContentView(R.layout.activity_main);

이 부분에 실행할 xml파일을 지정해주면 된다.

 

activity_main.xml 파일

레이아웃을 설정 해 줄 수 있다. 우측 상단의 Code를 누르면 설정한 레이아웃에 대한 코드가 나온다.

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Good Morning"
            android:textSize="30sp" />

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Click" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

jsp 처럼 각 항목에 대해 id를 줄 수 있고(android:id="@+id/줄id값")

여러 css도 지정해 줄 수 있다.

 

 

잘 작동되는 걸 확인할 수 있다.