-Activity 2개 만들어서 버튼 누르면 이동하게 만들기
1. 첫 화면
2. Hello~ 를 입력하고 이동을 누르면
3. 다른 창이 뜨고 메인으로 가고싶으면 버튼 누르기
4. 메인으로 돌아옴
<code>
- 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" >
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="텍스트를 입력하세요" />
<Button
android:id="@+id/btnMove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이동" />
</LinearLayout>
-activity_sub.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=".SubActivity"
android:orientation="vertical">
<Button
android:id="@+id/btnBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="메인으로 돌아가기" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="textView" />
</LinearLayout>
(textView 와 btnBack이 빨간색으로 나타났는데 오류없이 실행됐음.)
-SubActivity.java
package com.example.intentexample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SubActivity extends AppCompatActivity {
Button btnBack;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
textView = findViewById(R.id.textView);
btnBack = findViewById(R.id.btnBack);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish(); //Main나옴
}
});
Intent intent = getIntent();
String str = intent.getStringExtra("str");
textView.setText(str);
}
}
(textView 와 btnBack이 빨간색으로 나타났는데 오류없이 실행됐음.)
-MainActivity.java
package com.example.intentexample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button btnMove;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText =findViewById(R.id.editText);
btnMove = findViewById(R.id.btnMove);
btnMove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tmpStr = editText.getText().toString();
Intent intent = new Intent(MainActivity.this, SubActivity.class);
intent.putExtra("str", tmpStr);
editText.setText("");
startActivity(intent);
}
});
}
}
'Android' 카테고리의 다른 글
안드로이드 수업 2일차 (버튼 누르면 사진 랜덤으로 출력) (0) | 2020.10.27 |
---|---|
안드로이드 수업1일차 -실습 (0) | 2020.10.26 |
안드로이드 수업 1일차 -버튼 (0) | 2020.10.26 |
안드로이드 수업 1일차 (0) | 2020.10.26 |
Android Studio 4.1 Installation (0) | 2020.10.22 |