Android startActivityForResult基本用法

startActivityForResult和startActivity的区别是一个是带值传输,一个是直接跳转

具体用法如下:

activity_main:



    

    
activity_main1:



    

MainActivity:

package com.example.demo1;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView textview;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById(R.id.bt).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(MainActivity.this,
						SecondActivity.class), 1);
			}
		});
		textview=(TextView) findViewById(R.id.textview);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == 4) {
			String s=data.getStringExtra("bian");
			textview.setText(s);
		}
	}

}
SecondActivity:

package com.example.demo1;

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;

public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main1);
	final EditText text1=(EditText) findViewById(R.id.text1);
	Button finish=(Button) findViewById(R.id.finish);
	finish.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent i=new Intent();
			i.putExtra("bian", text1.getText().toString());
			setResult(4,i);
			finish();
		}
	});
}
}

别忘了注册

  


你可能感兴趣的:(安卓类)