1. strings.xml
<resources>
<string name="app_name">hello_string_name</string>
<string name="app_name2">화요일_name2</string>
<string name="app_name3">나는 이영신이다_name3</string>
</resources>
2. 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"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:text="새 화면 띄우기"
android:onClick="onButtonClicked2"/>
</RelativeLayout>
3. MainActivity.java
package com.example.hello_string;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
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);
}
//메서드 추가
public void onButtonClicked(View v){
Toast.makeText(getApplicationContext(),"버튼을 눌렀습니다",Toast.LENGTH_LONG).show();
}
//메서드 추가
public void onButtonClicked2(View v){
Intent intent=new Intent(getApplicationContext(),NewActivity.class);
startActivity(intent);
}
}
화면 실행 후, 또다른 화면 띄우기(NewActivity.class 로 나타내기!)
4. activity_new.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
>
<Button
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:text="되돌아가기"
android:onClick="onBackButtonClicked"/>
</LinearLayout>
5. NewActivity.java
package com.example.hello_string;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class NewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
//메서드 추가
public void onBackButtonClicked(View v){
Toast.makeText(getApplicationContext(),"back버튼 눌렀습니다",Toast.LENGTH_LONG).show();
finish();
}
}
6. manifests.xml에 activity 등록
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello_string">
<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.Hello_string">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewActivity">
</activity>
</application>
</manifest>
안드로이드 - 자바로부터 안드로이드 스튜디오까지 (0) | 2021.08.02 |
---|