«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

ITGenerations

안드로이드 - intent 본문

프로그래밍/안드로이드특강

안드로이드 - intent

ITGenerations 2018. 1. 24. 15:52

Java1-(main)

package com.example.ryan.intent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

static final int GET_STRING=1;
TextView text1;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
text1 = (TextView)findViewById(R.id.text1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent in = new Intent(MainActivity.this, sub.class);
startActivityForResult(in,GET_STRING);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode==GET_STRING){
if(resultCode==RESULT_OK){
text1.setText(data.getStringExtra("INPUT_TEXT"));
}
}
}

}


Java2

package com.example.ryan.intent;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class sub extends MainActivity {
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub);
edit = (EditText)findViewById(R.id.edit);

Button button_ok = (Button)findViewById(R.id.button_ok);
button_ok.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent intent = new Intent();
intent.putExtra("INPUT_TEXT",edit.getText().toString());
setResult(RESULT_OK,intent);
finish();
}
});

Button button_cancel=(Button) findViewById(R.id.button_cancel);
button_cancel.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
setResult(RESULT_CANCELED);
finish();
}
});



}
}

xml1-(main)

<?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"
tools:context="com.example.ryan.intent.MainActivity">

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="서브 액티비티로부터 문자열 반환받기"
/>

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="반환된 문자열" />

<TextView
android:id="@+id/text1"
android:layout_width="133dp"
android:layout_height="58dp"
android:hint="입력"
android:text="입력:ㅇㅇㄴㄹㅁㅇㄹ" />
</LinearLayout>

xml2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/linearLayout1"
android:layout_width="match_parent"

android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">


<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="93dp"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/button_ok"
android:layout_width="136dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="입력" />

<Button
android:id="@+id/button_cancel"
android:layout_width="141dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="취소" />
</LinearLayout>
</LinearLayout>

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ryan.intent">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".sub" android:label="sub"></activity>
</application>

</manifest>