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 57 58 59 60 61 62 63 64 65
|
package org.isgreat.loloof64.android.simple_intent_result_example;
import java.lang.ref.WeakReference;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ReadingActivity extends Activity {
public static final int EDIT_CONTENTS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reading);
Button editButton = (Button) findViewById(R.id.edit_button);
editButton.setOnClickListener(new EditButtonClickListener(this));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == EDIT_CONTENTS){
if(resultCode == RESULT_OK){
TextView firstNameView = (TextView) findViewById(R.id.first_name);
TextView lastNameView = (TextView) findViewById(R.id.last_name);
TextView ageView = (TextView) findViewById(R.id.age);
String firstNameField = getString(R.string.first_name_field);
String lastNameField = getString(R.string.last_name_field);
String ageField = getString(R.string.age_field);
String firstName = data.getStringExtra("first_name");
String lastName = data.getStringExtra("last_name");
String age = data.getStringExtra("age");
firstNameView.setText(firstNameField+firstName);
lastNameView.setText(lastNameField+lastName);
ageView.setText(ageField+age);
}
}
}
private static class EditButtonClickListener implements OnClickListener {
private final WeakReference<Activity> mParentActivity;
public EditButtonClickListener(Activity parentActivity){
mParentActivity = new WeakReference<Activity>(parentActivity);
}
public void onClick(View view){
Activity activity = mParentActivity.get();
Intent callingEditingViewIntent = new Intent(activity, WritingActivity.class);
activity.startActivityForResult(callingEditingViewIntent, EDIT_CONTENTS);
}
}
} |
Partager