activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

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

<Button

android:id="@+id/btn_next"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="NEXT"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="input text"

android:id="@+id/EditText_input"

android:layout_below="@id/btn_next"

</RelativeLayout>


activity_sub.xml


<?xml version="1.0" encoding="utf-8"?>

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

<Button

android:id="@+id/btn_prev"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="PREV"/>

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/TextView_get"

android:layout_below="@id/btn_prev"

</RelativeLayout>


MainActivity.java


package com.example.intenttext;

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 btn_next;

EditText EditText_input;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn_next = (Button)findViewById(R.id.btn_next);

EditText_input = (EditText)findViewById(R.id.EditText_input);

btn_next.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, SubActivity.class);

//intent.putExtra("t", EditText_input.getText().toString());

Bundle bundle = new Bundle();

bundle.putString("t2", EditText_input.getText().toString());

intent.putExtras(bundle);

startActivity(intent);

}

});

}

}


SubActivity.java


package com.example.intenttext;

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 btn_prev;

TextView TextView_get;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_sub);

btn_prev = (Button)findViewById(R.id.btn_prev);

TextView_get = (TextView)findViewById(R.id.TextView_get);

Intent intent = getIntent();

Bundle bundle = intent.getExtras();

String str = bundle.getString("t2");

TextView_get.setText(str);

btn_prev.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

finish();

}

});

}

}


[안드로이드 with 인크레파스] 11강_Intent

https://youtu.be/AdJ05ZPJcpE

 

 

 

 

 

#안드로이드, #스튜디오, #앱, #어플, #만들기, #intent, #bundle, #java, #xml

+ Recent posts