从Activity传递数据:在实际应用中,不仅要向Activity传递数据,还要从Activity返回数据,虽然返回数据和传递数据类似,也可以采用传递数据的四种方式,但是一把使用Intent对象的方式返回数据,使用这种方式返回数据,需要使用startActivityForResult方法来显示Activity
1、MainActivity
package com.example.intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends Activity {
private Button button1;
private EditText num1,num2,result;
private static final int REQUESTCODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button) findViewById(R.id.button1);
num1=(EditText) findViewById(R.id.num1);
num2=(EditText) findViewById(R.id.num2);
result=(EditText) findViewById(R.id.result_re);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int a=Integer.parseInt(num1.getText().toString());
int b=Integer.parseInt(num2.getText().toString());
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("num1", a);
intent.putExtra("num2",b);
startActivityForResult(intent, REQUESTCODE);//这里使用startActivityForResult启动Activity
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==2){
if(requestCode==REQUESTCODE){
int three=data.getIntExtra("result", 0);
result.setText(String.valueOf(three));
}
}
}
}
OtherActivity
package com.example.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OtherActivity extends Activity{
private TextView text;
private EditText user_result;
private Button button_re;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
text=(TextView) findViewById(R.id.text);
user_result=(EditText) findViewById(R.id.user_result);
button_re=(Button) findViewById(R.id.user_return);
Intent intent=getIntent();
int a=intent.getIntExtra("num1", 0);
int b=intent.getIntExtra("num2", 0);
text.setText(a+"+"+b+"="+"?");
button_re.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent2=new Intent();
int result=Integer.parseInt(user_result.getText().toString());
intent2.putExtra("result",result);
setResult(2, intent2);//这里的ResultCode的值一定要和MainActivity里面的ResultCode一致
finish();//要先结束这个Activity
}
});
}
}
activity_main.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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/num1"
android:layout_width="0px"
android:layout_weight="2"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="+"/>
<EditText
android:id="@+id/num2"
android:layout_width="0px"
android:layout_weight="2"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="="/>
<EditText
android:id="@+id/result_re"
android:layout_width="0px"
android:layout_weight="2"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请求结果"/>
</LinearLayout>
other.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/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/user_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/user_return"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="返回结果"/>
</LinearLayout>
结果:
实现返回数据的三步:
(1)在MainActivity调用startActivityForResult()方法,Requestcode可以自己定义
(2)在被调用的(OtherActivity)中返回数据时用setResult(ResultCode,data)方法进行传递,ResultCode一定要和MainActivity中的一致
(3)在MainActivity调用onAvtivityResult()方法对返回结果做处理
需要注意的几点:
(1)把字符串转化为int型,要用Integer.parse(String)方法
(2)但是数字转化为String不能直接用toString()方法,要用String.value