wasup

Android) 텍스트와 버튼 추가해보기 본문

IT/Android

Android) 텍스트와 버튼 추가해보기

wasupup 2021. 8. 3. 11:30
반응형

만들화면


소스코드

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="@string/app_name"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="@string/app_name2"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="110dp"
        android:text="@string/app_name3"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="140dp"
        android:text="버튼을 클릭하면 메세지가 출력됩니다."
        android:onClick="onButtonClicked"
        />
</RelativeLayout>

string.xml

<resources>
    <string name="app_name">hello_string</string>
    <string name="app_name2">hello_string2</string>
    <string name="app_name3">hello_string3</string>
</resources>

MainActivity.java

package com.example.hello_string;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    //mothod-add
    public void onButtonClicked(View v){
        Toast.makeText(getApplicationContext(),"버튼을 눌렀습니다.", Toast.LENGTH_LONG).show();
    }
}

클릭시결과


반응형

'IT > Android' 카테고리의 다른 글

Android) LinearLayout(리니어레이아웃)  (0) 2021.08.03
Android) 버튼 추가해보기  (0) 2021.08.03
Android) 새 화면 추가해보기  (0) 2021.08.03
Android) New Project  (0) 2021.08.03
Android) jdk, tomcat, Eclipse, android설치하기  (0) 2021.08.02
Comments