Android的startActivityForResult用法简单介绍

转载注明出处:http://blog.csdn.net/loveyaozu/article/details/51881401


Activity的跳转方式有两种,一种是startActivity直接跳转,另一种是startActivityForResult,带有数据回传的跳转。


新建一个FirstActivity类

package com.example.androidtest1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author syz
 * @date 2016-7-11
 */
public class FirstAcivity extends Activity {

	// 请求码
	public static final int REQUEST_CODE = 0X100;
	
	public static final String CONTENT="content";
	
	private TextView resultTv;
	private Button btCommit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_first);
		resultTv = (TextView) findViewById(R.id.content);
		btCommit = (Button) findViewById(R.id.go_second);
		btCommit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FirstAcivity.this,SecondActivity.class);
				FirstAcivity.this.startActivityForResult(intent, REQUEST_CODE);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == REQUEST_CODE){
			if(resultCode == RESULT_OK){
				String text = data.getStringExtra(CONTENT);
				if(!TextUtils.isEmpty(text)){
					resultTv.setText(text);
				}else{
					resultTv.setText("来自SecondActivity的CONTENT值为空");
				}
			}
		}else{
			
		}
	}
}


对应布局activity_first.xml

<?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" >
    

    <TextView 
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:layout_gravity="center"/>
    
    <Button 
        android:id="@+id/go_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="跳转到SecondActivity"/>
</LinearLayout>

另一个Activity类SecondActivity。设置数据并回传给FirstActivity

package com.example.androidtest1;

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.EditText;

/**
 * @author syz
 * @date 2016-7-11
 */
public class SecondActivity extends Activity {
	
	private Button mbutton;
	private EditText editText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		mbutton = (Button) findViewById(R.id.commit);
		editText = (EditText) findViewById(R.id.content);
		mbutton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String content = editText.getText().toString();
				Intent data = new Intent();
				data.putExtra(FirstAcivity.CONTENT, content);
				SecondActivity.this.setResult(RESULT_OK, data);
				finish();
			}
		});
	}

}


与之对应的布局文件activity_second.xml。主要是一个输入框和一个Button,输入框中输入的内容传递给FirstActivity。Button用于finish页面。

<?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/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp" />

    <Button
        android:id="@+id/commit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="提交" />

</LinearLayout>

效果展示

 








你可能感兴趣的:(android)