android startActivityForResult----给activity传值(返回值)

 

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button
android:text="Intent with Result"
android:onClick="onClickIntentWithResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

 



</LinearLayout>

package xiao.huang;

 

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class IntentActivity extends Activity {
private static final int REQUEST_CODE_1 = 101;
private static final int REQUEST_CODE_2 = 102;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent_test);
}

 

public void onClickImplicitIntent(View v) {
Uri number = Uri.parse("tel:01012345678");
Intent dial = new Intent(Intent.ACTION_DIAL, number);
startActivity(dial);
}

 

public void onClickIntentWithResult(View v) {
Intent intent = new Intent(this, WithResultActivity.class);
startActivityForResult(intent, REQUEST_CODE_1);
}

 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == Activity.RESULT_OK){
if (requestCode == REQUEST_CODE_1) {
int resultInt = data.getIntExtra("result", 0);
Toast.makeText(this, "RESULT=" + resultInt, Toast.LENGTH_SHORT).show();
}
else if (requestCode == REQUEST_CODE_2) {
String resultString = data.getStringExtra("result");
Toast.makeText(this, "RESULT=" + resultString, Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show();
}

}
}

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Ok"
android:onClick="onClickOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Cancel"
android:onClick="onClickCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Ok"
android:onClick="onClickOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Cancel"
android:onClick="onClickCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

 

posted @ 2011-12-08 17:29  xiaohuang  阅读(341)  评论(0)    收藏  举报