2016.05.31
구성
activity_main.xml
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 32 33 34 35 36 37 38 39 | ?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/edit" android:layout_width="match_parent" android:layout_height="wrap_content" > <requestFocus> </requestFocus> </EditText> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" > <Button android:id="@+id/button_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="입력완료" > </Button> <Button android:id="@+id/button_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="취소" > </Button> </LinearLayout> </LinearLayout> | cs |
activity_sub.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:textSize="25dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="받은 문자열" > </TextView> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="확인" > </Button> </LinearLayout> | cs |
MainActivity.java
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.example.user150226.a20160531ex2; 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; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final int GET_STRING = 1; EditText editText; Button button_ok, button_cancel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.edit); Button button_ok = (Button) findViewById(R.id.button_ok); Button button_cancel = (Button) findViewById(R.id.button_cancel); button_ok.setOnClickListener(this); button_cancel.setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId()==R.id.button_ok) { Intent intent = new Intent(MainActivity.this, SubActivity.class); intent.putExtra("edit", editText.getText().toString()); startActivityForResult(intent, GET_STRING); } else { finish(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode==GET_STRING) { if (resultCode==RESULT_OK) { editText.setText(""); String edit = data.getExtras().getString("edit").toString(); Toast.makeText(this, "이전에 받았던 문자는" + edit + "입니다.", Toast.LENGTH_SHORT).show(); } } } } | cs |
SubActivity.java
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 32 33 34 35 36 37 38 39 40 41 42 | package com.example.user150226.a20160531ex2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class SubActivity extends Activity { String edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); Intent intent = getIntent(); edit = intent.getExtras().getString("edit").toString(); TextView textView1 = (TextView) findViewById(R.id.textView1); textView1.setText("받아온 문자는 " + edit); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process(); finish(); } }); } private void process() { Intent intent = new Intent(this, MainActivity.class); intent.putExtra("edit", edit); setResult(Activity.RESULT_OK, intent); } } | cs |
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.user150226.a20160531ex2"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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=".SubActivity"></activity> </application> </manifest> | cs |
*실행결과
'Android' 카테고리의 다른 글
Android Studio 명시적 인텐트 값의 전달 (0) | 2016.05.31 |
---|---|
Android Studio 뷰 컨테이너 (0) | 2016.05.30 |
Android Studio 날짜/시간 관련 위젯 (0) | 2016.05.30 |
Android Studio 레이아웃 연습2 (0) | 2016.05.27 |
Android Studio 레이아웃 연습 (0) | 2016.05.27 |