액티비티간의 통신

[Codejava]
package yell.exam01;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Exam01Activity extends Activity {

TextView mText;
final static int ACT_EDIT = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);

mText = (TextView) findViewById(R.id.textView1);
}

public void modifyText(View v) {
switch (v.getId()) {
case R.id.btnModify:
Intent intent = new Intent(this, Exam01_modifyText.class);
intent.putExtra(“TextIn”, mText.getText().toString());
startActivityForResult(intent, ACT_EDIT);
break;

default:
break;
}
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACT_EDIT:
if (resultCode == RESULT_OK) {
mText.setText(data.getStringExtra(“TextOut”));
}
break;

default:
break;
}
}

public void showSubActivity(View v) {
Intent intent = new Intent(this, Exam01SubActivity.class);
startActivity(intent);
}
}
[/code]

[code java]
package yell.exam01;

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

public class Exam01_modifyText extends Activity {

EditText mEdit;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.modifytext);

mEdit = (EditText) findViewById(R.id.editText1);

Intent intent = getIntent();
String text = intent.getStringExtra(“TextIn”);
if (text != null) {
mEdit.setText(text);
}
}

public void mOnClick(View v) {
switch (v.getId()) {
case R.id.btnOk:
Intent intent = new Intent();
intent.putExtra(“TextOut”, mEdit.getText().toString());
setResult(RESULT_OK, intent);
finish();
break;

default:
setResult(RESULT_CANCELED);
finish();
break;
}
}
}
[/code]

답글 남기기