EditText 입력값 다른 Activity 에서 출력하기

반응형

다른곳도 수정해야 하지만 파란색도 빠트리지 말고 수정해줍니다.


Code for MainActivity.java file.

package com.passedittextvalue_android_examples.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

 EditText SendValue;
 Button SendEditTextValue;
 Intent intent;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 SendEditTextValue = (Button)findViewById(R.id.button1);
 SendValue = (EditText)findViewById(R.id.editText1);
 
 SendEditTextValue.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 intent = new Intent(getApplicationContext(),SecondActivity.class);
 intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString());
 startActivity(intent);
 
 }
 });
 }
}

Code for activity_main.xml layout file.

<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"
 tools:context="com.passedittextvalue_android_examples.com.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:ems="10" >
 </EditText>

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="26dp"
 android:text="Click Here to Pass edittext value to another activity in android" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Main Activity"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Code for SecondActivity.java file.

package com.passedittextvalue_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {

 TextView receive;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_second);
 
 receive = (TextView)findViewById(R.id.textView1);
 
 receive.setText(getIntent().getStringExtra("EdiTtEXTvALUE"));
 
 }
}

Code for activity_second.xml layout file.

 <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" tools:context="com.passedittextvalue_android_examples.com.SecondActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="22dp" android:text="Second Activity" android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>

Screenshot:

Pass EditText value to another activity in android



Click Here to download Pass EditText value to another activity in android project.


[출처] https://www.android-examples.com/pass-edittext-value-to-another-activity-in-android/


반응형

댓글()