您的位置:首页 > 移动开发 > Android开发

[Android]意图之回传数据

2016-05-05 22:35 501 查看
前面有篇博文介绍过通过意图把前一个Activity的数据传给后面的Acitivity,本篇文章实现从后面的Activity传值给前面的Activity。即:从后往前传值。

1、在activity_main布局文件中,添加以下组件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText android:id="@+id/one"
android:layout_width="60dp"
android:layout_height="wrap_content">
</EditText>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+">
</TextView>
<EditText android:id="@+id/two"
android:layout_width="60dp"
android:layout_height="wrap_content">
</EditText>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="=">
</TextView>
<EditText android:id="@+id/result"
android:layout_width="60dp"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="计算结果"
/>
</LinearLayout>


2、在MainActivity中,获取one,two两个文本框的值,并且通过意图传递给下一个activity。

package com.example.intentvalueback;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
private Button button;
// 返回结果码
private final static int REQUEST_CODE = 1;
private EditText one,two,result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

one = (EditText)this.findViewById(R.id.one);
two = (EditText)this.findViewById(R.id.two);
result = (EditText)this.findViewById(R.id.result);

button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int a = Integer.parseInt(one.getText().toString());
int b = Integer.parseInt(two.getText().toString());

Intent intent = new Intent(MainActivity.this,Other.class);

// 把a,b值传到Other
intent.putExtra("a", a);
intent.putExtra("b", b);
// 表示启动意图可以返回结果
startActivityForResult(intent, REQUEST_CODE);
}
});
}

// 当other返回的时候andriod回调此方法
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
// TODO Auto-generated method stub
super.onActivityResult(arg0, arg1, arg2);
// arg1 表示resultCode arg0表示requestCode
if(arg1 == 2) {
if(arg0 == REQUEST_CODE) {
int three = arg2.getIntExtra("three", 0);
result.setText(String.valueOf(three));
}
}
}
}


3、新建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" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/three"/>
</LinearLayout>
<Button android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回结果"/>
</LinearLayout>


4、从Other中得到MainActivity的one,two值。并显示在TextView中。

通过意图将result文本框的值回传给MainActivity。

package com.example.intentvalueback;

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 Other extends Activity {
private Button button;
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(com.example.intentvalueback.R.layout.other);
button = (Button)this.findViewById(com.example.intentvalueback.R.id.button2);
textView = (TextView)this.findViewById(com.example.intentvalueback.R.id.msg);
editText = (EditText)this.findViewById(R.id.three);
Intent intent = getIntent();
// 获取从前面的activity传过来的值
int a = intent.getIntExtra("a", 0);
int b = intent.getIntExtra("b", 0);
textView.setText(a +  " + " + b + "=" + "?");

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
int three = Integer.parseInt(editText.getText().toString());
System.out.print("three----->" + three);
intent.putExtra("three", three);
// 通过Intent返回结果,嗲用setResult方法,2代表数据回传的标记位,要与前面一个acitvity的request_code一致
setResult(2,intent);
// 调用finish()结束当前的acitivity的生命周期
finish();

}
});

}
}


注意:

a、在MainActivity中,使用startActivityForResult方法,表示启动的意图可以返回结果。这个方法接收两个参数,第一个参数表示当前的意图,第二个参数代表请求码。

b、在MainActivity中,当Other返回的时候andriod回调此方法:onActivityResult。在此方法中,我们首先判断结果码,是否跟Other中定义的结果是否一致,一般来说这个结果码只要是大于1的整数就可以,这里统一定义为2.接着根据请求码判断当前的请求是否跟启动意图时定义的请求码一致,一致则表示当前请求的结果是从第二个activity回传过来的。

c、在Other中,首先获取用户输入的计算结果,重新声明一个意图把计算结果通过此意图传递给上一个activity。具体是通过:intent.putExtra(“three”, three);来实现。接着,通过Intent返回结果,调用setResult方法,这个方法的第一个参数要跟MainActivity中定义个结果码要一致(这里定义为2),最后调用finish()方法结束Other的生命周期。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息