ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [안드로이드 스튜디오] 서브 액티비티로부터 문자열 반환받기
    프로그래밍/XML | JAVA 2024. 6. 9. 19:35


    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:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical"
        android:gravity="center">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="문자열 반환 받기">
        </Button>
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="반환된 문자열">
        </TextView>
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <EditText
            android:id="@+id/edittext1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </EditText>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center">
            <Button
                android:id="@+id/bt_submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="입력 완료"></Button>
            <Button
                android:id="@+id/bt_cancle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="취소"></Button>
        </LinearLayout>
    </LinearLayout>

    JAVA

    package com.example.a06_exercise;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import androidx.activity.EdgeToEdge;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.graphics.Insets;
    import androidx.core.view.ViewCompat;
    import androidx.core.view.WindowInsetsCompat;
    
    public class MainActivity extends AppCompatActivity {
        TextView text1;
        Button button1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            EdgeToEdge.enable(this);
            setContentView(R.layout.activity_main);
    
            text1 = findViewById(R.id.text1);
            button1 = findViewById(R.id.button1);
    
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    startActivityForResult(intent, 0);
                }
            });
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                String str = data.getStringExtra("text");
                text1.setText(str);
            }
        }
    }
    package com.example.a06_exercise;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    import androidx.activity.EdgeToEdge;
    import androidx.appcompat.app.AppCompatActivity;
    
    public class SecondActivity extends AppCompatActivity {
        EditText editText1;
        Button bt_submit, bt_cancle;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            editText1 = findViewById(R.id.edittext1);
            bt_submit = findViewById(R.id.bt_submit);
            bt_cancle = findViewById(R.id.bt_cancle);
    
            bt_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(SecondActivity.this, MainActivity.class);
                    intent.putExtra("text", editText1.getText().toString());
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
    
            bt_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editText1.setText("");
                }
            });
        }
    }

Designed by Tistory.