ITGenerations
안드로이드 - intent 본문
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>
'프로그래밍 > 안드로이드특강' 카테고리의 다른 글
안드로이드 - 터치 (0) | 2018.01.23 |
---|---|
안드로이드 - 버튼, 새로운 창에 새로운 버튼 클릭 (0) | 2018.01.23 |
버튼상자생성 - 빨강, 녹색, 파랑 중 선택 (0) | 2018.01.23 |
버튼 yes, no (0) | 2018.01.23 |
계산기 소스 (0) | 2018.01.22 |