[Android] 안드로이드 스튜디오intent 이용하여 text 보내기

    메인화면과 서브 화면이 있어야 하므로 xml2개 자바파일 2개로 구성

     

    우선 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"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">
    
    <EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Input Message"
    />
    
    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Push"
    android:textColor="#000000"
    android:textSize="28dp"
    />
    
    </LinearLayout>

     

    서브 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"
    android:orientation="vertical"
    android:gravity="center"
    android:background="#ccccFF"
    >
    
    <EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Input Message"
    />
    
    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Back"
    android:textSize="28dp"
    android:textColor="#0000FF"
    />
    
    </LinearLayout>

     

    xml파일은 그냥 화면에 보여주는 이미지이기 때문에 단순하다. 

     

    Main 자바파일

    package com.example.screenchange2;
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    
    public class MainActivity extends AppCompatActivity {
    public static final int REQUEST_CODE =1;
    public static final String TAG_MSG = "message";
    
    private EditText editText;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    editText = (EditText)findViewById(R.id.editText);                  // //데이터?를 받아야 하기 때문에 만들어준다.
    Button button = (Button)findViewById(R.id.button);              //데이터?를 받아야 하기 때문에 만들어준다.
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this,SubActivity.class);             //intent 생성
    
    String msg = editText.getText().toString();
    editText.setText("");                                                       //빈공간에 InputMessage라고 뜬다.
    intent.putExtra(TAG_MSG,msg);                                        //빈공간에 데이터 (문자) 넣기
    startActivityForResult(intent, REQUEST_CODE);                      //값을 돌려받을수있다.
    }
    });
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)      //서브에서 입력된 editText 돌려받기
    {
    super.onActivityResult(requestCode,resultCode,data);
    String msg = data.getStringExtra(TAG_MSG);
    editText.setText(msg);
    }
    }

     

     

    sub자바파일

    package com.example.screenchange2;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    
    public class SubActivity extends AppCompatActivity {
    
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sub);
    
    Intent intent = getIntent();                                                    //intent에서 data를 꺼낸다.
    
    String msg = intent.getStringExtra(MainActivity.TAG_MSG);
    
    editText = (EditText)findViewById(R.id.editText);
    editText.setText(msg);
    
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent = new Intent();
    
    String msg = editText.getText().toString();
    intent.putExtra(MainActivity.TAG_MSG, msg);
    setResult(RESULT_OK, intent);
    finish();
    }
    });
    }
    }

     

    만들어진 이미지

     

     

    qwer을 입력후 push버튼을 누르면

    이렇게 두번째 화면에서 qwer이 보여지게 된다. 

    여기서도 문자를 입력후 back을 누르게 되면 동일하게 첫번째화면 (Main) 에서도 보이게 된다.

    댓글

    Designed by JB FACTORY