Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text" />
<Button
android:id="@+id/activity_btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="이동"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.activityex03;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity1 extends Activity {
InputMethodManager imm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
findViewById(R.id.activity_btn1).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText et = (EditText)findViewById(R.id.et);
String msg = et.getText().toString();
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
Intent intent = new Intent(MainActivity1.this, MainActivity2.class);
intent.putExtra("msg", msg);
//startActivity(intent);
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
//super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
String result = "결과 : " + data.getStringExtra("result");
TextView tv2 = (TextView)findViewById(R.id.tv2);
tv2.setText(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity1, menu);
return true;
}
}
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/activity_btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="닫기" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.activityex03;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
Intent intent = getIntent();
String msg = intent.getStringExtra("msg");
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(msg);
findViewById(R.id.activity_btn2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("result", "1234");
setResult(RESULT_OK,intent);
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity2, menu);
return true;
}
}
'Android' 카테고리의 다른 글
PreferenceActivity (0) | 2013.04.04 |
---|---|
Preference (0) | 2013.04.04 |
액티비티(activity) - (0) | 2013.04.03 |
대화상자 - 커스텀대화상자 (0) | 2013.04.02 |
대화상자 - MultiChoiceItems (0) | 2013.04.02 |